2

我正在做一些非常简单的事情。我正在尝试从一个小的 XML 文件中检索一个节点。

 <?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>

我正在尝试获取 ServiceProvider。以下是我拥有的 C# 代码:

字符串父 = "ServiceProviderProfiles"; 字符串孩子=“服务提供者”;

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

当我调试时, xmlNode 为空。我的代码有什么问题导致 xmlNode 为空?

4

1 回答 1

0

有多种方法可以做到这一点。

这是示例代码

 // 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();
于 2017-09-08T00:56:15.553 回答