Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择XML节点_C#_Xml_Xmldocument_Xmlnode_Selectsinglenode - Fatal编程技术网

C# 选择XML节点

C# 选择XML节点,c#,xml,xmldocument,xmlnode,selectsinglenode,C#,Xml,Xmldocument,Xmlnode,Selectsinglenode,我正在做一些非常简单的事情。我试图从一个小XML文件中检索一个节点 <?xml version="1.0" encoding="UTF-8" ?> <SAMLConfiguration xmlns="urn:componentspace:SAML:2.0:configuration"> <IdentityProvider Name="IdpNameInSPForIsuer" Description="SecureAut

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

我正在尝试获取服务提供商。 下面是我的C代码:

字符串parent=“ServiceProviderProfiles”; string children=“ServiceProvider”

var nodePath=string.Concat(@//,父,@/,子)
var xmlNode=xmlDocument。选择SingleNode(nodePath)


调试时,xmlNode为null。我的代码有哪些问题导致xmlNode为空?

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

下面是示例代码

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

您需要使用默认名称空间
xmlns=“urn:componentspace:SAML:2.0:configuration”
进行搜索,如和.Sample using所示。答案是updategreat!谢谢你的更新。快乐编码!