locked
Xml - Reading child elements RRS feed

  • Question

  •  

    Hi,

    Could someone suggest me how to read all the Xml child elements

    and check if they have child elements generically?

     

    How can I get to know that this element <grandchild ID="2"/> doesn't have an end element?

     

    <root>

    <parent ID="100">

    <child ID="10">

    <grandchild ID="1">Hello world<grandchild/>

    <grandchild ID="2"/>

    </child>

    <child ID="20">

    </child>

    </parent>

    <parent ID="200">

    <child ID="10">

    </child>

    <child ID="20">

    </child>

    </parent>

    </root>

     

    Regards,

    John

     

    Tuesday, August 14, 2007 11:57 AM

Answers

  • You could loop through those child elements and check whether the number of child elements they have is zero. For the second problem you can check the IsEmpty property of those grandchild elements, it is true if the element is marked up with an emtpy element tag and false otherwise. The following C# code sample does what I suggest:

     

    Code Snippet

          XmlDocument xmlDocument = new XmlDocument();
          xmlDocument.Load(@"file.xml");
          foreach (XmlElement child in xmlDocument.SelectNodes("root/parent/child"))
          {
            Console.WriteLine("child with id {0} has {1} child element(s).", child.GetAttribute("ID"), child.SelectNodes("*").Count);
          }
          foreach (XmlElement grandchild in xmlDocument.SelectNodes("root/parent/child/grandchild"))
          {
            Console.WriteLine("grandchild with id {0} is marked up with empty element tag: {1}.", grandchild.GetAttribute("ID"), grandchild.IsEmpty);
          }

     

     

    I have choosen XPath expressions like root/parent/child according to your XML sample, if you want a more generic apporach then you could as well use */*/* for instance.
    Tuesday, August 14, 2007 12:28 PM
  • Here is a .NET 2.0 C# sample using XmlReader and checking elements of name 'child' for the number of descendant elements and elements inside that of name 'grandchild' for the empty element form:

    Code Snippet

    using (XmlReader xmlReader = XmlReader.Create(@"file.xml"))

    {

    while (xmlReader.Read())

    {

    switch (xmlReader.NodeType)

    {

    case XmlNodeType.Element:

    switch (xmlReader.Name)

    {

    case "child":

    int childCount = 0;

    string id = xmlReader.GetAttribute("ID");

    while (xmlReader.Read() && !(xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name == "child"))

    {

    if (xmlReader.NodeType == XmlNodeType.Element)

    {

    childCount++;

    if (xmlReader.Name == "grandchild")

    {

    Console.WriteLine("grandchild with id {0} is in empty element form: {1}.", xmlReader.GetAttribute("ID"), xmlReader.IsEmptyElement);

    }

    }

    }

    Console.WriteLine("child element with id {0} has {1} child element(s).", id, childCount);

    break;

    }

    break;

    }

    }

    }

     

     

    Tuesday, August 14, 2007 2:02 PM

All replies

  • You could loop through those child elements and check whether the number of child elements they have is zero. For the second problem you can check the IsEmpty property of those grandchild elements, it is true if the element is marked up with an emtpy element tag and false otherwise. The following C# code sample does what I suggest:

     

    Code Snippet

          XmlDocument xmlDocument = new XmlDocument();
          xmlDocument.Load(@"file.xml");
          foreach (XmlElement child in xmlDocument.SelectNodes("root/parent/child"))
          {
            Console.WriteLine("child with id {0} has {1} child element(s).", child.GetAttribute("ID"), child.SelectNodes("*").Count);
          }
          foreach (XmlElement grandchild in xmlDocument.SelectNodes("root/parent/child/grandchild"))
          {
            Console.WriteLine("grandchild with id {0} is marked up with empty element tag: {1}.", grandchild.GetAttribute("ID"), grandchild.IsEmpty);
          }

     

     

    I have choosen XPath expressions like root/parent/child according to your XML sample, if you want a more generic apporach then you could as well use */*/* for instance.
    Tuesday, August 14, 2007 12:28 PM
  • Hi Martin,

     

    Thanks for the reply.

     

    Could you please help me to do the same with XmlTextReader too?

     

    Regards,

    John

     

    Tuesday, August 14, 2007 1:03 PM
  • Here is a .NET 2.0 C# sample using XmlReader and checking elements of name 'child' for the number of descendant elements and elements inside that of name 'grandchild' for the empty element form:

    Code Snippet

    using (XmlReader xmlReader = XmlReader.Create(@"file.xml"))

    {

    while (xmlReader.Read())

    {

    switch (xmlReader.NodeType)

    {

    case XmlNodeType.Element:

    switch (xmlReader.Name)

    {

    case "child":

    int childCount = 0;

    string id = xmlReader.GetAttribute("ID");

    while (xmlReader.Read() && !(xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.Name == "child"))

    {

    if (xmlReader.NodeType == XmlNodeType.Element)

    {

    childCount++;

    if (xmlReader.Name == "grandchild")

    {

    Console.WriteLine("grandchild with id {0} is in empty element form: {1}.", xmlReader.GetAttribute("ID"), xmlReader.IsEmptyElement);

    }

    }

    }

    Console.WriteLine("child element with id {0} has {1} child element(s).", id, childCount);

    break;

    }

    break;

    }

    }

    }

     

     

    Tuesday, August 14, 2007 2:02 PM
  • Hi Martin,

    Thank you very much for both the code snippets.

    They are very helpful to me.

     

    Regards,

    John

     

    Wednesday, August 15, 2007 7:54 PM