Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > XML parsing problem finding a specific element in a specific place

Reply
Thread Tools

XML parsing problem finding a specific element in a specific place

 
 
mazdotnet
Guest
Posts: n/a
 
      10-01-2009
Hi, I have the following XML and I like to grab the node that is in
'List2' and mobilenum=416-222-5435

I have tried the following,
XmlNode xmlNode = xmlDoc.SelectSingleNode(@"/contacts/list
[@name='List2']/contact/@mobilenum=416-222-5435");
and
XmlNode xmlNode = xmlDoc.SelectSingleNode(@"/contacts/list
[@name='List2']/contact/@mobilenum='416-222-5435'");
but I get an error 'Expression must evaluate to a node-set.' Any
idea?

<contacts>
<list name="list1">
<contact>
<name>Name1</name>
<mobilenum>416-223-3345</mobilenum>
<email>(E-Mail Removed)</email>
<profession>Web Manager</profession>
</contact>
</list>
<list name="List2">
<contact>
<name>Name2</name>
<mobilenum>416-222-5435</mobilenum>
<email>email</email>
<professionbbbb</profession>
</contact>
<contact>
<name>Name</name>
<mobilenum>416-223-2342</mobilenum>
<email>someemail</email>
<profession>aaaa</profession>
</contact>
</list>
</contacts>
Thanks
Maz.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      10-02-2009
mazdotnet wrote:
> Hi, I have the following XML and I like to grab the node that is in
> 'List2' and mobilenum=416-222-5435
>
> I have tried the following,
> XmlNode xmlNode = xmlDoc.SelectSingleNode(@"/contacts/list
> [@name='List2']/contact/@mobilenum=416-222-5435");
> and
> XmlNode xmlNode = xmlDoc.SelectSingleNode(@"/contacts/list
> [@name='List2']/contact/@mobilenum='416-222-5435'");
> but I get an error 'Expression must evaluate to a node-set.' Any
> idea?


Use the XPath
/contacts/list[@name = 'List2']/contact[mobilenum = '416-222-5435']


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      10-02-2009
On Oct 1, 8:13*pm, mazdotnet <(E-Mail Removed)> wrote:
> Hi, I have the following XML and I like to grab the node that is in
> 'List2' and mobilenum=416-222-5435
>
> I have tried the following,
> XmlNode xmlNode = xmlDoc.SelectSingleNode(@"/contacts/list
> [@name='List2']/contact/@mobilenum=416-222-5435");
> and
> XmlNode xmlNode = xmlDoc.SelectSingleNode(@"/contacts/list
> [@name='List2']/contact/@mobilenum='416-222-5435'");
> but I get an error 'Expression must evaluate to a node-set.' Any
> idea?
>
> <contacts>
> * <list name="list1">
> * * <contact>
> * * * <name>Name1</name>
> * * * <mobilenum>416-223-3345</mobilenum>
> * * * <email>(E-Mail Removed)</email>
> * * * <profession>Web Manager</profession>
> * * </contact>
> * </list>
> * <list name="List2">
> * * <contact>
> * * * <name>Name2</name>
> * * * <mobilenum>416-222-5435</mobilenum>
> * * * <email>email</email>
> * * * <professionbbbb</profession>
> * * </contact>
> * * <contact>
> * * * <name>Name</name>
> * * * <mobilenum>416-223-2342</mobilenum>
> * * * <email>someemail</email>
> * * * <profession>aaaa</profession>
> * * </contact>
> </list>
> </contacts>
> Thanks
> Maz.


If you know that list name is "List2" then why do you need to look for
416-222-5435? Just use

XmlNode xmlNode = xmlDoc.SelectSingleNode(@"/contacts/list
[@name='List2']");
string num = xmlNode.SelectSingleNode("mobilenum").InnerText;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Parsing DOM to search specific tags with specific custom attribute William FERRERES Javascript 7 07-09-2007 08:11 PM
Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file jkflens XML 2 05-30-2006 09:41 AM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Problem in parsing XML file with empty element anthony Perl Misc 3 11-16-2005 03:17 PM



Advertisments