Selecting an XML Node

218 views Asked by At

I am doing something very simple. I'm trying to retrieve a node from a small XML file.

 <?xml version="1.0"  encoding="UTF-8" ?>
<SAMLConfiguration xmlns="urn:componentspace:SAML:2.0:configuration">
  <IdentityProvider Name="IdpNameInSPForIsuer"
                    Description="SecureAuth"
                    LocalCertificateFile=""
                    LocalCertificatePassword=""/>

  <ServiceProviderProfiles>
    <ServiceProvider NameIdentifier ="SPIssuerName"
                     ExpectsSignatureVerification="true"
                     ExpectsSignedResponse="false"
                     Certificate="sharedpubliccsert.cer"
                     DigestMethod="SAMLIdentifiers.DigestMethods.SHA1"
                     SignatureMethod="SAMLIdentifiers.SignatureMethods.RSA_SHA1"
                     SingleLogoutServiceUrl="https://serviceprovider/slo"
                         SendResponseBy="HTTP-Redirect" />

  </ServiceProviderProfiles>
</SAMLConfiguration>

I am trying to get the ServiceProvider. Below is the C# code I have:

string parent = "ServiceProviderProfiles"; string children = "ServiceProvider";

var nodePath = string.Concat(@"//", parent, @"/", children);
var xmlNode = xmlDocument.SelectSingleNode(nodePath);

When I debug, the xmlNode is null. What problems with my code is causing the xmlNode to be null?

1

There are 1 answers

2
AudioBubble On BEST ANSWER

There are various ways for doing this.

Here is sample code

 // xmlns attribute from the root
 XNamespace ns = "urn:componentspace:SAML:2.0:configuration";
 // read XML file into XmlDocument
 XDocument doc = XDocument.Load("file.xml");
 // Select XML descendants  with Linq
 var result = doc.Descendants(ns + "SAMLConfiguration").Descendants().Where(c => c.Name.LocalName.ToString() == "ServiceProvider")
           .ToArray();