Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used xmlDocument :

C#
XmlDocument xml = new XmlDocument();
                           xml.LoadXml(text); // suppose that myXmlString contains "<Names>...</Names>"

                           XmlNodeList xnList = xml.SelectNodes("Etichetta");
                           foreach (XmlNode xn in xnList)
                           {
                               string firstName = xn["Barcode"].InnerText;
                               string lastName = xn["Materiale"].InnerText;

                           }




but when I run code I have this error: An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

What I have tried:

I tried to use XDocument but I don't know I can write to find my root, my file xml is
Posted
Updated 11-Apr-23 9:39am
v3
Comments
0x01AA 5-Apr-23 9:56am    
1. XmlNodeList xnList = xml.SelectNodes("Etichetta");
should be XmlNodeList xnList = xml.SelectNodes("PrintDymo/Etichetta");
2. Barcode and Materiale are Attributes of _individual_ Campi nodes, which at least for me makes not big sense.
PIEBALDconsult 5-Apr-23 10:03am    
or xml.SelectNodes("//Etichetta")
or xml.SelectNodes("*/Etichetta") (maybe?)
0x01AA 5-Apr-23 10:13am    
Thanks for this. I'm not experienced with XPath, that's why I did not post a solution but only a comment. At least I tested it before posting ;)
Member 14594285 5-Apr-23 10:16am    
I don't understand, I have always the same error
PIEBALDconsult 5-Apr-23 10:18am    
Yes, well, I may be able to assist in a bit.
Do you have no control over the format of the XML file?

Try this code, which will list the values of the Campi attributes.
C#
XmlDocument xml = new XmlDocument();
XmlTextReader reader = new XmlTextReader("Filename.xml");
xml.Load(reader);

XmlNodeList xnList = xml.SelectNodes("PrintDymo/Etichetta");
foreach (XmlNode xn in xnList)
{
    XmlNodeList campiList = xn.SelectNodes("Campi");
    foreach (XmlNode cn in campiList)
    {
        XmlAttributeCollection attribs = cn.Attributes;
        Console.WriteLine($"attribute: {attribs[0].Value}");
    }
}

The Campi elements do not contain anything except a single attribute, which as others have pointed out, does not make a lot of sense. An element should contain all valid attrivbutes together, something like:
XML
<Campi Barcode ="123456789012" Data ="05/04/2023" Materiale="Pellame"/>


[edit]
Changed to SelectNodes.
[/edit]

[edit]
With Campi entries correctly formatted try:
C#
XmlNodeList xnList = xml.SelectNodes("PrintDymo/Etichetta/Campi");;
foreach (XmlNode xn in xnList)
{
    string firstName = xn.Attributes["Barcode"].InnerText;
    string lastName = xn.Attributes["Materiale"].InnerText;
    Console.WriteLine($"firstName: {firstName}, lastName: {lastName}");
}


[/edit]
 
Share this answer
 
v3
Comments
PIEBALDconsult 5-Apr-23 10:26am    
Ew :: makes the sign of the Cross at "GetElementsByTagName" ::
Richard MacCutchan 5-Apr-23 10:28am    
Yes, I know, but I am not an XML expert like you. I mainly wanted to demonstrate why the code and the XML are not a good match.
Richard MacCutchan 5-Apr-23 10:40am    
Better?
PIEBALDconsult 5-Apr-23 10:46am    
Sigh of relief.
Member 14594285 5-Apr-23 11:21am    
thanks
Hi
Here is a solution by using LINQ to xml

C#
XDocument doc = XDocument.Load("YourData.xml");

var CampiList = (from e in doc.Descendants("Etichetta")
                 let c = e.Elements("Campi")
                 select new
                 {
                     Barcode = c.Attributes("Barcode").First().Value,
                     Data = c.Attributes("Data").First().Value,
                     Materiale = c.Attributes("Materiale").First().Value
                 }).ToList();

foreach(var campi in CampiList)
{
    Console.WriteLine($"firstname {campi.Barcode} lastname {campi.Materiale}.");
}
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900