user2566049
user2566049

Reputation: 23

SelectNodes returning 0 elements

I am losing my mind with namespaces. I cannot for the life of me figure out how to use SelectNodes after following most posts on how to do it.

My xml:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment>
    <ComponentGroup Id="ProgramFiles">
      <Component ..../>
      <Component ..../>
      <Component ..../>

What I've tried:

 xnManager = new XmlNamespaceManager(doc.NameTable);
 xnManager.AddNamespace("wx", "http://schemas.microsoft.com/wix/2006/wi");

 //XmlNodeList aNodes = doc.SelectNodes("//wx:Wix/wx:Fragment/wx:ComponentGroup/Component", xnManager);

 //var aNodes = doc.GetElementsByTagName("wx:Wix/Fragment/ComponentGroup/Component");

 //System.Xml.XmlNodeList aNodes = doc.SelectNodes("//*[local-name()=\"Component\"]");

What am I missing? All results return 0 elements. The document is valid and loaded into doc.

Upvotes: 0

Views: 121

Answers (1)

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22187

Please try the following conceptual example.

It is using LINQ to XML API.

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
        <Fragment>
            <ComponentGroup Id='ProgramFiles'>
                <Component>One</Component>
                <Component>Two</Component>
                <Component>Three</Component>
            </ComponentGroup>
        </Fragment>
    </Wix>");

    XNamespace ns = xdoc.Root.GetDefaultNamespace();
    foreach (XElement xelem in xdoc.Descendants(ns + "Component"))
    {
        Console.WriteLine(xelem.Value);
    }
}

Output

One
Two
Three

Upvotes: 1

Related Questions