XmlDocument document = new XmlDocument();
document.Load("sample.xml");
XmlElement root = document.DocumentElement;
XmlNode node = root.SelectSingleNode("//menu[id=b]");
Console.Write( node.SelectSingleNode("//menu[id=b]/row[id=1]/vis").Value );
Console.Write( node.SelectSingleNode("//menu[id=b]/row[id=0]/vis").Value );
For ...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<menus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<menu>
<id>a</id>
<row>
<id>0</id>
<vis>Alpha</vis>
</row>
<row>
<id>1</id>
<vis>Beta</vis>
</row>
</menu>
<menu>
<id>b</id>
<row>
<id>0</id>
<vis>Gamma</vis>
</row>
<row>
<id>1</id>
<vis>Epsilon</vis>
</row>
</menu>
</menus>
... Thom
___________________________________________________
Thom Little - www.tlanet.net - Thom Little Associates, Ltd.
What do you get ?
My guess would be that you want:
Console.Write( root.SelectSingleNode("//menu[id=b]/row[id=1]/vis").Value );
Console.Write( root.SelectSingleNode("//menu[id=b]/row[id=0]/vis").Value );
to search from root instead of node.
Arne
The first problem that I see is that you first select the second menu
node and try to run an XPath query on this node as if it was a root. Try
to change your second and third XPath query to something like:
"/row[id=x]/vis"
Best regards!
--
Marcin Hoppe
Email: marcin...@gmail.com
Blog: http://devlicio.us/blogs/marcin_hoppe
Additional information: Object reference not set to an instance of an
object.
With your change (if I did it correctly) I get the same result.
An unhandled exception of type 'System.NullReferenceException' occurred in
ConsoleXML.exe
Additional information: Object reference not set to an instance of an
object.
With your change (if I did it correctly) I get the same result.
... Thom
You also missed some '' around values and to get the actual text.
Console.Write(
root.SelectSingleNode("//menu[id='b']/row[id='1']/vis/text()").Value );
Console.Write(
root.SelectSingleNode("//menu[id='b']/row[id='0']/vis/text()").Value );
works for me.
Arne
Hi Thom,
Apart from the excellent replies that you already have, given that you
are using C# 3.5, maybe you should also think about using Linq for XML,
I personally find that this is the absolute best way to work with XML,
not just the query syntax, but the whole new xml API that does not work
with a DOM, sooo much easier.
In this case, one way do do this would be....
XElement xe = XElement.Load(@"C:\Temp\Test.xml");
var elements = from menu in xe.DescendantsAndSelf("menu")
where (string)menu.Element("id") == "b"
from row in menu.Descendants("row")
select row;
StringBuilder sb = new StringBuilder();
foreach (XElement x in elements)
{
sb.Append((string)x.Element("vis"));
}
MessageBox.Show(sb.ToString());
Regards Tim.
--
Opps, didn't notice the ordering...so you need to add an Orderby
var elements = from menu in xe.DescendantsAndSelf("menu")
where (string)menu.Element("id") == "b"
from row in menu.Descendants("row")
orderby (int)row.Element("id") descending
select row;
--
Yes when I add the single quotes and change "value" to "InnerXml" it does
EXACTLY what I need.
I was reading over it over, and over, and over.
Thank you very much for the help.
This project is going through three phases and I will try out your
suggestion on the next phase.
Thanks again.