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

  • Home
  • SEARCH
  • 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 6123699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:02:50+00:00 2026-05-23T16:02:50+00:00

Having an XML document, I want to get the first node with a certain

  • 0

Having an XML document, I want to get the first node with a certain name, no matter in which nesting depth it is contained.

I tried several things without success:

var node1 = doc.SelectSingleNode(@"//Shortcut");
var node2 = doc.SelectSingleNode(@"/*/Shortcut");
var node3 = doc.SelectSingleNode(@"//*/Shortcut");
var node4 = doc.SelectSingleNode(@"*/Shortcut");
...

Each call results in a NULL node.

I think it should be some trivial XPath syntax. Can you help me?

(In case this matters: The XML document is an input file for a WiX project, so there could be some namespace issues involved?!?).

Edit

I also tried the following:

var nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(string.Empty, @"http://schemas.microsoft.com/wix/2006/wi");
nsm.AddNamespace(@"ns", @"http://schemas.microsoft.com/wix/2006/wi");

together with:

var node1 = doc.SelectSingleNode(@"//Shortcut", nsm);
var node2 = doc.SelectSingleNode(@"/*/Shortcut", nsm);
var node3 = doc.SelectSingleNode(@"//*/Shortcut", nsm);
var node4 = doc.SelectSingleNode(@"*/Shortcut", nsm);
...

Leading to the same results.

Edit 2 – Solution

I found the solution:

var nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(string.Empty, @"http://schemas.microsoft.com/wix/2006/wi");
nsm.AddNamespace(@"ns", @"http://schemas.microsoft.com/wix/2006/wi");

and then

var node1 = doc.SelectSingleNode(@"//ns:Shortcut", nsm);

This succeeded.

  • 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-23T16:02:50+00:00Added an answer on May 23, 2026 at 4:02 pm

    The XPath expression that selects exactly the wanted node (and nothing in addition) is:

    (//x:Shortcut)[1]
    

    So, using:

    doc.SelectNodes("(//x:Shortcut)[1]", someNamespaceManager)
    

    where

    the prefix "x" is bound to the namespace "http://schemas.microsoft.com/wix/2006/wi"

    in someNamespaceManager

    This has an advantage over the proposed solution (to use SelectSingleNode()), because it can easily be adjusted to select the N-th wanted node in the XML document.

    For example:

       (//x:Shortcut)[3]
    

    selects the 3rd (in document order) x:Shortcut element, and

       (//x:Shortcut)[last()]
    

    selects the last (in document order) x:Shortcut element in the XML document.

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

Sidebar

Related Questions

I have a reasonably complex XML document which I want to flatten down to
I am having one xml file and I want to parse it to get
I am having trouble creating an XML document that contains a default namespace and
I am having the below code. import xml.dom.minidom def get_a_document(name): return xml.dom.minidom.parse(name) doc =
I am having xml with following structure <ruleDefinition appId=3 customerId = acf> <node alias=element1
I am having on xml from which i have to remove the one book
I have many xml file's, I do not want to get the value, but
I'm having a couple of websites hosted different places, which I want to backup
I'm using PHP and CURL to get XML, but I'm having problems accessing a
I have to parse an xml document and have to get all the elements

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.