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 1010913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:09:20+00:00 2026-05-16T09:09:20+00:00

I’m using C# and .NET 2.0. Given the XML below, I’d like to get

  • 0

I’m using C# and .NET 2.0.

Given the XML below, I’d like to get a XMLNodeList of <user> nodes where the <role> is “admin”.

<users>
    <user>
        <name>John Doe</name>
        <roles>
            <role>superadmin</role>
            <role>admin</role>
        </roles>
    </user>
    <user>
        <name>Jane Doe</name>
        <roles>
            <role>superadmin</role>
            <role>admin</role>
        </roles>
    </user>
    <user>
        <name>Rob Doe</name>
        <roles>
            <role>support</role>
        </roles>
    </user>
 </users>

Assume roleName = “admin”. This works, but is case-sensitive.

userNodesForRole = _document.SelectNodes("//users/user[roles[role='" + roleName + "']]");

I want to do it in a case-insensitive fashion. I know I can’t use the matches function because .NET 2.0 (and maybe higher?) doesn’t support XPath 2.0. So I did this:

// abc...xyz is the string literal of the entire alphabet, of course
userNodesForRole = _document.SelectNodes("//users/user[roles[translate(role,'abc..xyz','ABC...XYZ')='" + roleName.ToUpper() + "']]", _xmlNamespaceManager);

However, I don’t get any nodes back. Can someone please tell me what I’m getting wrong?

  • 1 1 Answer
  • 0 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-16T09:09:21+00:00Added an answer on May 16, 2026 at 9:09 am

    _document.SelectNodes(“//users/user[roles[translate(role,’abc..xyz’,’ABC…XYZ’)='”
    + roleName.ToUpper() + “‘]]”, _xmlNamespaceManager);

    This evaluates an XPath expression like the following:

      //users/user
               [roles
                 [translate(role,
                            'abcdefghijklmnopqrstuvxyz',
                            'ABCDEFGHIJKLMNOPQRSTUVXYZ'
                            )
                 =
                  'ADMIN'
                  ]
               ]
    

    and the above XPath expression when evaluated on the provided XML document:

    <users>
        <user>
            <name>John Doe</name>
            <roles>
                <role>admin</role>
            </roles>
        </user>
        <user>
            <name>Jane Doe</name>
            <roles>
                <role>admin</role>
            </roles>
        </user>
        <user>
            <name>Rob Doe</name>
            <roles>
                <role>support</role>
            </roles>
        </user>
     </users>
    

    selects the following nodes:

    <user>
        <name>John Doe</name>
        <roles>
            <role>admin</role>
        </roles>
    </user>
    
    <user>
        <name>Jane Doe</name>
        <roles>
            <role>admin</role>
        </roles>
    </user>
    

    The XPath expression will not select the wanted elements if there is a roles element that has more than one role child and a <role>admin</role> element is not the first role child of its parent.

    An XPath expression that selects the desired elements even in this case is;

      //users/user
               [roles/role
                 [translate(.,
                            'abcdefghijklmnopqrstuvxyz',
                            'ABCDEFGHIJKLMNOPQRSTUVXYZ'
                            )
                 =
                  'ADMIN'
                  ]
               ]
    

    There may be a variety of reasons why the first expression raises an exception or doesn’t select the wanted nodes. To find out the exact reason, print the constructed XPath expression and provide it here.

    Update: @ck has now provided his real XML document and indeed, in the real XML document the role children ( <role>admin</role> elements ) arenot the first role child of their parent.

    So my guess and explanation was correct.

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

Sidebar

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.