Following console application example demonstrates a way to get <desc>
element that is direct child of <section>
element, also to get <name>
and <desc>
under each <diag>
element. I'm using XDocument from System.Xml.Linq
. But not using linq query syntax as it isn't necessary in this case and you seems doesn't like it :
//load xml string
var doc = XDocument.Parse(xml);
//or use XDocument.Load("path_to_xml_file.xml"); to load from file
//get all <section> element
var sections = doc.Root.Elements("section");
//get <desc> and all <diag> under each <section>
foreach (var section in sections)
{
var desc = (string)section.Element("desc");
Console.WriteLine(String.Format("new Section. Desc: {0}", desc));
var diags = section.Descendants("diag");
foreach (var diag in diags)
{
var name = (string)diag.Element("name");
var desc2 = (string)diag.Element("desc");
Console.WriteLine(String.Format("name: {0}, desc: {1}", name, desc2));
}
}
notice that Descendants()
is used to get any child of current element and Element()
used to get only direct child of current element.