XPath navigator

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?
 
M

miher

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top