2012-10-30 51 views
1

加載從XML的具體節點我想從以下XML如何使用C#

<t:RequestSecurityTokenResponse xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"> 
    <t:RequestedSecurityToken> 
    <Assertion ID="_a2baec61-1e5a-4f7e-8cc4-44ca3cb82a44" IssueInstant="2012-10-30T11:56:19.211Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion"> 
     <Issuer>https://vidizmo1-ac.accesscontrol.windows.net/</Issuer> 
     <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
     <ds:SignedInfo> 
      <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> 
      <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /> 
      <ds:Reference URI="#_a2baec61-1e5a-4f7e-8cc4-44ca3cb82a44"> 
      <ds:Transforms> 
       <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
       <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> 
      </ds:Transforms> 
      <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /> 
      <ds:DigestValue>XsW696+Y/CkArU5a8zvqeEckl+rpgiv0lEEs2PDx3Hw=</ds:DigestValue> 
      </ds:Reference> 
     </ds:SignedInfo> 

     </ds:Signature> 

     **<AttributeStatement> 
     <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"> 
      <AttributeValue>[email protected]</AttributeValue> 
      </Attribute> 
      <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"> 
      <AttributeValue>farooq test</AttributeValue> 
      </Attribute> 
      <Attribute Name="http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider"> 
      <AttributeValue>Google</AttributeValue> 
      </Attribute> 
     </AttributeStatement>** 

    </Assertion> 
    </t:RequestedSecurityToken> 
    <t:TokenType>urn:oasis:names:tc:SAML:2.0:assertion</t:TokenType> 
    <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType> 
    <t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType> 
</t:RequestSecurityTokenResponse> 

和我的代碼得到的負荷:

string xml = @"C:\Users\baseer.haider\Documents\visual studio 2010\Projects\CheckReadXML\CheckReadXML\ACS.xml"; 
      XmlDocument acsXml = new XmlDocument(); 
      acsXml.Load(xml); 
      XmlNodeList acsNode = acsXml.DocumentElement.SelectNodes("//AttributeStatement//Attribute"); 

      foreach (XmlNode node in acsNode) 
      { 
       if (node.Attributes["Name"] != null) 
       { 
        if (node.Attributes["Name"].Value == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress") 
        { 
         var innerNode = node.SelectSingleNode("//AttributeValue"); 
         Console.WriteLine(innerNode.InnerText); 
        } 

       } 

我想從以下XML負載的節點但我不能加載它,因爲當我選擇該節點使用此代碼 XmlNodeList acsNode = acsXml.DocumentElement.SelectNodes("//AttributeStatement//Attribute");

acsNode爲空。 任何機構可以修復這個PLZ ??

回答

1

使用LINQ2XML

XElement doc = XElement.Load("yourStream.xml"); 
XNamespace t="http://schemas.xmlsoap.org/ws/2005/02/trust"; 
XNamespace a="urn:oasis:names:tc:SAML:2.0:assertion"; 

var lstElmVal=doc.Descendants(t+"RequestedSecurityToken") 
.Descendants(t+"Assertion") 
.Element(a+"AttributeStatement") 
.Elements(a+"Attribute") 
.Where(x=>x.Attribute("Name").Value=="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress") 
.Select(y=>y.Value); 

//lstElmVal now contains your required data 
1

使用Linq2Xml要容易得多。

XDocument xDoc = XDocument.Load(fileName); 
XNamespace ns = "urn:oasis:names:tc:SAML:2.0:assertion"; 

var attvals = xDoc.Descendants(ns + "AttributeValue") 
        .Select(x => x.Value) 
        .ToList();