【问题标题】:Selecting an XML Node [duplicate]选择 XML 节点 [重复]
【发布时间】:2018-02-16 19:16:28
【问题描述】:

我正在做一些非常简单的事情。我正在尝试从一个小的 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# 代码:

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

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

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

【问题讨论】:

标签: c# xml xmldocument xmlnode selectsinglenode


【解决方案1】:

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

这里是示例代码

 // 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();

【讨论】:

  • 答案已更新
  • 太棒了!感谢您的更新。编码快乐!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多