如何使用C#从XML加载特定节点

时间:2012-10-30 12:57:38

标签: c# xml xml-parsing

我希望从XML

中获得负载
<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>farooqtest11@gmail.com</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为空。 任何人都可以解决这个问题吗?

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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