XML parsing problem finding a specific element in a specific place

Discussion in 'ASP .Net' started by mazdotnet, Oct 1, 2009.

  1. mazdotnet

    mazdotnet 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>
    <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.
     
    mazdotnet, Oct 1, 2009
    #1
    1. Advertising

  2. Re: XML parsing problem finding a specific element in a specificplace

    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/
     
    Martin Honnen, Oct 2, 2009
    #2
    1. Advertising

  3. Re: XML parsing problem finding a specific element in a specificplace

    On Oct 1, 8:13 pm, 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?
    >
    > <contacts>
    >   <list name="list1">
    >     <contact>
    >       <name>Name1</name>
    >       <mobilenum>416-223-3345</mobilenum>
    >       <email></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;
     
    Alexey Smirnov, Oct 2, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jkflens
    Replies:
    2
    Views:
    1,515
    jkflens
    May 30, 2006
  2. HANM
    Replies:
    2
    Views:
    735
    Joseph Kesselman
    Jan 29, 2008
  3. anthony
    Replies:
    3
    Views:
    254
    anthony
    Nov 16, 2005
  4. Erik Wasser
    Replies:
    5
    Views:
    484
    Peter J. Holzer
    Mar 5, 2006
  5. William FERRERES
    Replies:
    7
    Views:
    226
    William FERRERES
    Jul 9, 2007
Loading...

Share This Page