Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 206949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:41:31+00:00 2026-05-11T17:41:31+00:00

My XML looks like : <?xml version=\1.0\?> <itemSet> <Item>one</Item> <Item>two</Item> <Item>three</Item> …..maybe more Items

  • 0

My XML looks like :

<?xml version=\"1.0\"?>
<itemSet>
       <Item>one</Item>
       <Item>two</Item>
       <Item>three</Item>
       .....maybe more Items here.
</itemSet>

Some of the individual Item may or may not be present. Say I want to retrieve the element <Item>two</Item> if it’s present. I’ve tried the following XPaths (in C#).

  • XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']") — If Item two is present, then it returns me only the first element one. Maybe this query just points to the first element in itemSet, if it has an Item of value two somewhere as a child. Is this interpretation correct?

So I tried:

  • XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']/Item[1]") — I read this query as, return me the first <Item> element within itemSet that has value = ‘two’. Am I correct?

This still returns only the first element one. What am I doing wrong?
In both the cases, using the siblings I can traverse the child nodes and get to two, but that’s not what I am looking at. Also if two is absent then SelectSingleNode returns null. Thus the very fact that I am getting a successfull return node does indicate the presence of element two, so had I wanted a boolean test to chk presence of two, any of the above XPaths would suffice, but I actually the need the full element <Item>two</Item> as my return node.

[My first question here, and my first time working with web programming, so I just learned the above XPaths and related xml stuff on the fly right now from past questions in SO. So be gentle, and let me know if I am a doofus or flouting any community rules. Thanks.]

  • 1 1 Answer
  • 6 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:41:31+00:00Added an answer on May 11, 2026 at 5:41 pm

    I think you want:

    myXMLdoc.SelectSingleNode("/itemSet/Item[text()='two']")
    

    In other words, you want the Item which has text of two, not the itemSet containing it.

    You can also use a single dot to indicate the context node, in your case:

    myXMLdoc.SelectSingleNode("/itemSet/Item[.='two']")
    

    EDIT: The difference between . and text() is that . means “this node” effectively, and text() means “all the text node children of this node”. In both cases the comparison will be against the “string-value” of the LHS. For an element node, the string-value is “the concatenation of the string-values of all text node descendants of the element node in document order” and for a collection of text nodes, the comparison will check whether any text node is equal to the one you’re testing against.

    So it doesn’t matter when the element content only has a single text node, but suppose we had:

    <root>
      <item name="first">x<foo/>y</item>
      <item name="second">xy<foo/>ab</item>
    </root>
    

    Then an XPath expression of “root/item[.='xy']” will match the first item, but “root/item[text()='xy']” will match the second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
My xml looks like: <nodes> <node name=somekey> <item name=subject>blah</item> <item name=body>body</item> </node> </nodes> And
My xml looks like this- <?xml version=1.0 encoding=UTF-8?> <root> <Objects> <object1>object1Value</object1> <object2>object1Value</object2> </Objects> </root>
I use Netbeans IDE. My hibernate.cfg.xml looks like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE hibernate-configuration
Here's what part of my ivy.xml looks like right now: <dependency org=org.springframework name=org.springframework.core rev=3.0.2.RELEASE
My XML looks like this: <?xml version = 1.0 encoding = utf-8?> <gallery> <name>Rosie's
I have a XML file that looks like this <Licence> <Name>Test company</Name> <Version>1.1.1.1</Version> <NumberOfServer>2</NumberOfServer>
My XML file looks something like this: <MyXml> <Version> 9.3.2 </Version> <Resources> <Sets> <ItemCollection>
I have xml input which looks like (simplified version used for example): <Student> <Subject>
I have hash(Hash.from_xml) which looks like this (from inspect): { FileName=>hofplayers.xml, Version=>1.0, UserID=>3955847, FetchedDate=>2011-08-16

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.