Rajeswari ML
Rajeswari ML

Reputation: 599

SelectNodes Always returning the 0 count

SelectNodes always returning the 0 count even the it has the value.

    <?xml version="1.0" encoding="utf-16"?>
    <Configurations xmlns="DEH_Common.Schemas">
      <sftpConfiguration>
        <file>
          <filedetails>
            <fileext>csv</fileext>
            <DataContentDetailId>1</DataContentDetailId>
          </filedetails>
    </file>
    </sftpConfiguration>
    </Configurations>

C# to read the nodelist....

XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("//Configurations/sftpConfiguration/file");

Upvotes: 1

Views: 1458

Answers (3)

Mohammad Nikravesh
Mohammad Nikravesh

Reputation: 975

It's because of using namespace in your xml, You should add namespace to xmlDoc and also no need to useDocumentElement` This code would be work:

var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("a", "DEH_Common.Schemas");
XmlNodeList nodeList = xmlDoc.SelectNodes("//a:sftpConfiguration/a:file", nsmgr);

Upvotes: 4

Mohammad Nikravesh
Mohammad Nikravesh

Reputation: 975

Try this:

xmlDoc.DocumentElement.SelectNodes("/Configurations[@*]/sftpConfiguration/file");

Upvotes: 0

Cleptus
Cleptus

Reputation: 3541

In your XML DocumentElement is the Configurations node, so your XPath should be sftpConfiguration/file

XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("sftpConfiguration/file");

Upvotes: 1

Related Questions