Hi,
I've been trying to retrieve some nodes by node name, using wildcards, but have failed miserably! So, I wondered if anyone had done this before and could give me some pointers? Here's a description of what I'm trying to acheive:
Assume I have some xml that looks like this:
I need to use C# to retrieve all the node values where the node name begins with "name" (i.e. name1, name2 and name3)
Thanks in advance!!
StoolzPosted Oct 18, 2007, 4:49 AM
Apologies! It does work - perfectly!!
I'd missed out the single quotes around the name! Ooops!
Many thanks for your help.
StoolzPosted Oct 18, 2007, 4:46 AM
Hello again,
I tried this but unfortunately it seems to be ignoring the node name criteria and just selecting all nodes under settings (i.e. the same as "settings/*")
Any other ideas?
Thanks again!
StoolzPosted Oct 17, 2007, 5:31 AM
Hi Alan,
Thank you so much for your reply - this looks exactly what I wanted!
I will give it a try!
Think I need to improve my XPath skills - it's something I've never got to grips with!!
Thanks again,
Nathan.
AlanPosted Oct 16, 2007, 12:46 PM
Try this:
XmlDocument doc = new XmlDocument();
doc.Load("stoolz.xml"); // or whatever
XmlNodeList nodeList = doc.SelectNodes("//settings/*[starts-with(name(),'name')]");
foreach (XmlNode node in nodeList)
{
string text = node.InnerText;
Console.WriteLine("Name: {0}",node.Name);
Console.WriteLine("Contents: {0}",node.InnerText);
Console.WriteLine();
}