Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML reading child nodes

Tags:

c#

xml

I have an XML document and I am trying to get the childnodes of an element called Unit

My XML Doc is layed out like so:

<Unit>
    <id>3</id>
    <name>System Information</name>
    <description>null</description>
    ... other ...
</Unit>

This is the code I am using to try and read them.

public void Load()
{
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    XmlDocument xmldoc = new XmlDocument();
    XmlNodeList xmlnode;

    xmldoc.Load(fs);
    xmlnode = xmldoc.GetElementsByTagName("Units");

    for (int i = 0; i < xmlnode.Count; i++)
    {
        string str = string.Format("ID: {0}\r\nName:{0}", xmlnode[i].ChildNodes.Item(0).InnerText, xmlnode[i].ChildNodes.Item(1).InnerText);
        MessageBox.Show(str);
    }
}

But the problem is, when I try and get them, instead of getting item 0 or item 1, it displays all the items and not the items i have selected.

like image 274
Ben Avatar asked Feb 17 '13 10:02

Ben


3 Answers

As I can see from your xml and code. You have error in line:

 xmlnode = xmldoc.GetElementsByTagName("Units");

change it on:

 xmlnode = xmldoc.GetElementsByTagName("Unit");
like image 194
Maris Avatar answered Oct 06 '22 13:10

Maris


With Linq to Xml you can easily parse your xml into (anonymous) strongly typed objects:

public void Load()
{
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

    var xdoc = XDocument.Load(fs);
    var units = from u in xdoc.Descendants("Unit")
                select new {
                   Id = (int)u.Element("id"),
                   Name = (string)u.Element("name")
                };

    foreach(var unit in units)
    {
       // thanks God for IntelliSense!
       MessageBox.Show(String.Format("ID:{0}\r\nName:{1}", unit.Id, unit.Name));
    }
}

Well, actually if you only need to show those values in message box, you can even write all code in one line. But I prefer first approach, when retrieving data and displaying them are separated (ideally in separate methods):

XDocument.Load(filePath)
         .Descendants("Unit")
         .Select(u => String.Format("Id:{0}\nName:{1}", (int)u.Element("id"), (string)u.Element("name"))
         .ToList()
         .ForEach(MessageBox.Show);
like image 37
Sergey Berezovskiy Avatar answered Oct 06 '22 13:10

Sergey Berezovskiy


You have a typo in element name: should be Unit instead of Units.

However, have you tried using LINQ to XML instead of XmlDocument?

public void Load()
{
    var doc = XDocument.Load(filePath);

    foreach(var unit in doc.Descendants("Unit"))
    {
        string str = string.Format("ID: {0}\r\nName:{0}", unit.Element("id").Value, unit.Element("name").Value);
        MessageBox.Show(str);
    }
}
like image 39
MarcinJuraszek Avatar answered Oct 06 '22 12:10

MarcinJuraszek