XML parsing problem finding a specific element in a specific place

M

mazdotnet

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>[email protected]</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.
 
M

Martin Honnen

mazdotnet said:
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']
 
G

Guest

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>[email protected]</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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top