Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Select XML Node

0 views
Skip to first unread message

Thom Little

unread,
May 11, 2008, 6:18:35 PM5/11/08
to
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

unread,
May 11, 2008, 6:27:10 PM5/11/08
to
Thom Little wrote:
> 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 );

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

Marcin Hoppe

unread,
May 11, 2008, 6:30:24 PM5/11/08
to
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!
--
Marcin Hoppe
Email: marcin...@gmail.com
Blog: http://devlicio.us/blogs/marcin_hoppe

Thom Little

unread,
May 11, 2008, 6:52:47 PM5/11/08
to
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 Little

unread,
May 11, 2008, 6:55:12 PM5/11/08
to
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

Arne Vajhøj

unread,
May 11, 2008, 7:12:25 PM5/11/08
to
Thom Little wrote:
> 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

Tim Jarvis

unread,
May 11, 2008, 7:56:09 PM5/11/08
to
Thom Little wrote:

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

unread,
May 11, 2008, 7:58:46 PM5/11/08
to
Tim Jarvis wrote:

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

unread,
May 11, 2008, 8:18:49 PM5/11/08
to
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 Little

unread,
May 11, 2008, 8:23:37 PM5/11/08
to
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.

0 new messages