SelectNodes only works one level when xmlns is present


SelectNodes only works for one level when xmnls is present.  My xml file has a xmlns declaration and SelectNodes only works for one level.

My code:

                XmlNamespaceManager namespaceManager = new
                    XmlNamespaceManager(xmlDocument.NameTable);
                namespaceManager.AddNamespace("pf","urn:astm-org:CCR"    );
                nodeList =      xmlDocument.SelectNodes("//pf:CCRDocumentObjectID", namespaceManager);
This will return one node.

                nodeList = xmlDocument.SelectNodes("//ContinuityOfCareRecord/@pf:CCRDocumentObjectID", namespaceManager);
But the above statement will not return any node.

The following is fragment of my xml file.

< xml version="1.0" encoding="UTF-8" >
<ContinuityOfCareRecord xmlns="urn:astm-org:CCR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:astm-org:CCR CCR1.0.xsd">
    <CCRDocumentObjectID>DW2005-05-31T22:34:32Z</CCRDocumentObjectID>
</ContinuityOfCareRecord>

Any help will be appreciated.






Answer this question

SelectNodes only works one level when xmlns is present

  • Nathan Going

    Sure //ContinuityOfCareRecord/@pf:CCRDocumentObjectID won't select anything in your XML, because 1) ContinuityOfCareRecord element in your XML is in urn:astm-org:CCR namespace. And 2) it has no CCRDocumentObjectID attribute. What's wrong with the first selection

  • Jan Sten

    Thanks for your reply.

    For my previous example, it does not matter.  I have several complex queries which need to use more levels.

    I figured out how to make my query works.  The following query should works.

    //pf:ContinuityOfCareRecord/pf:CCRDocumentObjectID

  • SelectNodes only works one level when xmlns is present