C# Identify parent,child elements in XML file

Viewed 976

I found this on internet.

string xml = @"
<food>
  <child>
    <nested />
  </child>
  <child>
    <other>
    </other>
  </child>
</food>
";

XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xml));
while (rdr.Read())
{
  if (rdr.NodeType == XmlNodeType.Element)
  {
  Console.WriteLine(rdr.LocalName);
  }
}

The result of the above will be

food
child
nested
child
other

This is working perfect, Just i need to identify which elements contains child elements.

for example, I need this output

startParent_food
    startParent_child
        nested
    endParent_child
    startParent_child
        other
    endParent_child
endParent_food
2 Answers
Related