Maciek
Maciek

Reputation: 19893

XPATH SelectSingleNode returns null despite using a XmlNamespaceManager

I'm trying to test an XML document (SAML in this case) that looks like this:

<saml2p:LogoutRequest xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_bbcf6cc5-8832-4567-9a52-7baef04676d7" Version="2.0" IssueInstant="2014-10-21T13:24:54.1397367Z" NotOnOrAfter="2014-10-21T13:34:54.1397367Z">
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
            <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
            <Reference URI="#_bbcf6cc5-8832-4567-9a52-7baef04676d7">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                    <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>DigestValueGoesHere!</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>SignatureValueGoesHere!
        </SignatureValue>
        <KeyInfo>
            <X509Data>
              <X509Certificate>X509CertificateGoesHere!</X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>
</saml2p:LogoutRequest>

For that purpose, I've created some extensions which I'm trying to use to test the XML :

public static class XmlExtensions
    {
        public static XmlNode Signature(this XmlDocument document)
        {
            return document.SelectSingleNodeFromDocumentRoot(@"/saml2p:LogoutRequest/Signature");
        }

        private static XmlNode SelectSingleNodeFromDocumentRoot(this XmlDocument document, string path)
        {
            var ns = new XmlNamespaceManager(document.NameTable);

            ns.AddNamespace("saml2", @"urn:oasis:names:tc:SAML:2.0:assertion");
            ns.AddNamespace("saml2p", @"urn:oasis:names:tc:SAML:2.0:protocol");
            ns.AddNamespace("ns1", @"http://www.w3.org/2000/09/xmldsig#");

            var result = document.SelectSingleNode(path, ns);
            return result;
        }
    }

In one of my tests, I'm attempting to retrieve the /saml2p:LogoutRequest/Signature element, however my extensions are returning null. Why? I thought I should use a supplied ns manager with xml namespaces added to it? Please note that the query path works perfect in XPath Visualizer Tool.

Upvotes: 3

Views: 428

Answers (1)

StuartLC
StuartLC

Reputation: 107237

Signature is in namespace xmlns="http://www.w3.org/2000/09/xmldsig#", so you need to adjust:

return document.SelectSingleNodeFromDocumentRoot(@"/saml2p:LogoutRequest/ns1:Signature");

Upvotes: 3

Related Questions