代码之家  ›  专栏  ›  技术社区  ›  TheFootClan

选择XML节点[重复]

  •  2
  • TheFootClan  · 技术社区  · 7 年前

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

    string parent=“ServiceProviderProfiles”;

    var nodePath=字符串。连接(@“//”,父,@“/”,子);
    var xmlNode=xmlDocument。选择SingleNode(nodePath);

    1 回复  |  直到 7 年前
        1
  •  0
  •   user5158149 user5158149    7 年前

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

    这是示例代码

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