Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Select XML Node

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thom Little - 11 May 2008 23:18 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
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.