XPath navigator

  • Thread starter Thread starter Arne
  • Start date Start date
A

Arne

<?xml version="1.0" ?>
<emailtrigger>
<globals>
<shopurl>Some Data</shopurl>
<schoolName>Some school<schoolName>
</globals>
</emailtrigger>
I would like to write some codes that retrieves all the nodes in
emailtrigger/globals.
If I know the name of the nodes it would be easy. If the name of the nodes
are unknown I don't know how to iterated through the nodes.
What can I do?
 
Arne said:
<?xml version="1.0" ?>
<emailtrigger>
<globals>
<shopurl>Some Data</shopurl>
<schoolName>Some school<schoolName>
</globals>
</emailtrigger>
I would like to write some codes that retrieves all the nodes in
emailtrigger/globals.
If I know the name of the nodes it would be easy. If the name of the nodes
are unknown I don't know how to iterated through the nodes.
What can I do?

Hi,

I might not get Your problem right but heres 2 ways to list the elements
under globals.

// with System.Xml
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(File.ReadAllText("TestXmlFile.xml"));
foreach (XmlNode node in
doc1.SelectNodes("/emailtrigger/globals/*"))
Console.WriteLine("{0}:{1}", node.LocalName, node.InnerXml);

// with Linq2Xml
XDocument doc2 = XDocument.Load("TestXmlFile.xml");
var result = doc2.Root.Descendants("globals").Descendants()
.Select( node => new { ElementName = node.Name, ElementValue
= node.Value });
foreach (var item in result)
Console.WriteLine("{0}:{1}", item.ElementName,
item.ElementValue);

Hope You find this useful.
-Zsolt
 
Back
Top