Using C# 3.5 I want to select a value from an XML file. The intent of the
following is to print "EpsilonGamma". It does not work. Can you point out
my probably very obvious error?
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.
Arne Vajhøj - 11 May 2008 23:27 GMT
> Using C# 3.5 I want to select a value from an XML file. The intent of the
> following is to print "EpsilonGamma". It does not work. Can you point out
[quoted text clipped - 5 lines]
> 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 );
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.
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <menus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
[quoted text clipped - 21 lines]
> </menu>
> </menus>
Arne
Thom Little - 11 May 2008 23:52 GMT
When using just one Console.Write I get ...
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
___________________________________________________
Thom Little - www.tlanet.net - Thom Little Associates, Ltd.
Arne Vajhøj - 12 May 2008 00:12 GMT
> When using just one Console.Write I get ...
>
> 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.
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
Thom Little - 12 May 2008 01:18 GMT
Some Bits!
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.
... Thom
___________________________________________________
Thom Little - www.tlanet.net - Thom Little Associates, Ltd.
Marcin Hoppe - 11 May 2008 23:30 GMT
Thom Little pisze:
> Can you point out my probably very obvious error?
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!

Signature
Marcin Hoppe
Email: marcin.hoppe@gmail.com
Blog: http://devlicio.us/blogs/marcin_hoppe
Thom Little - 11 May 2008 23:55 GMT
When using just one Console.Write I get ...
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
___________________________________________________
Thom Little - www.tlanet.net - Thom Little Associates, Ltd.
Tim Jarvis - 12 May 2008 00:56 GMT
> Using C# 3.5 I want to select a value from an XML file. The intent
> of the following is to print "EpsilonGamma". It does not work. Can
[quoted text clipped - 40 lines]
> ___________________________________________________
> Thom Little - www.tlanet.net - Thom Little Associates, Ltd.
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.
--
Tim Jarvis - 12 May 2008 00:58 GMT
> > Using C# 3.5 I want to select a value from an XML file. The intent
> > of the following is to print "EpsilonGamma". It does not work. Can
[quoted text clipped - 67 lines]
>
> 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;
--
Thom Little - 12 May 2008 01:23 GMT
Thank you for the excellent suggestion.
This project is going through three phases and I will try out your
suggestion on the next phase.
Thanks again.
... Thom
___________________________________________________
Thom Little - www.tlanet.net - Thom Little Associates, Ltd.