XPATH SelectSingleNode returns null despite using XmlNamespaceManager
I am 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 this purpose, I have created some extensions that I am trying to use for testing 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 am trying to get an element /saml2p:LogoutRequest/Signature
, however my extensions are returning null. What for? I thought I should use the provided ns manager with the xml namespaces added? Note that the query path works fine in the XPath Visualizer Tool .
+3
source to share