从C#中的XML文件中提取特定节点

时间:2019-07-16 16:09:44

标签: c# .net xml nodes saml

我正在尝试使用C#从XML文件中提取特定节点。我想从此XML中仅提取userID(123456789)

<Response Destination="https://saml.qc.xxxx.com/sp/ACS.saml2" IssueInstant="2011-02-14T20:39:00.328Z" ID="iRTyBb7E9OLitdGZT1RYRSJNX85" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <saml:Issuer>some.client.com:sso
    </saml:Issuer>
    <Status>
        <StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </Status>
    <saml:Assertion Version="2.0" IssueInstant="2011-02-14T20:39:00.328Z" ID="YJtYu1HoChn0nrORzDSkVGOE8RD">
        <saml:Issuer>some.client.com:sso</saml:Issuer>
        <saml:Subject>
            <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">123456789</saml:NameID>
            <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
                <saml:SubjectConfirmationData NotOnOrAfter="2011-02-14T20:40:00.328Z" Recipient="https://saml.qc.xxxx.com/sp/ACS.saml2" />
            </saml:SubjectConfirmation>
        </saml:Subject>
        <saml:Conditions NotOnOrAfter="2011-02-14T20:40:00.328Z" NotBefore="2011-02-14T20:38:00.328Z">
            <saml:AudienceRestriction>
                <saml:Audience>saml.qc.xxxx.com:saml2.0</saml:Audience>
            </saml:AudienceRestriction>
        </saml:Conditions>
        <saml:AuthnStatement AuthnInstant="2011-02-14T20:39:00.328Z" SessionIndex="YJtYu1HoChn0nrORzDSkVGOE8RD">
            <saml:AuthnContext>
                <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Pwd</saml:AuthnContextClassRef>
            </saml:AuthnContext>
        </saml:AuthnStatement>
        <saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" Name="clientId">
                <saml:AttributeValue xsi:type="xs:string">99999</saml:AttributeValue>
            </saml:Attribute>
        </saml:AttributeStatement>
    </saml:Assertion>
</Response>

这是我的代码:

public static void ExtractUserID(XmlDocument Doc)
        {
            XmlElement docRoot = Doc.DocumentElement;

            XmlNodeList idNode = Doc.GetElementsByTagName("saml:NameID");

            Console.WriteLine("UserID: " + idNode);
            Console.ReadLine();
        }

输出:

UserID: System.Xml.XmlElementList

我想得到这样的东西:

User ID: 123456789

2 个答案:

答案 0 :(得分:0)

使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication120
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            string id = (string)doc.Descendants().Where(x => x.Name.LocalName == "NameID").FirstOrDefault();
         }
    }


}

答案 1 :(得分:0)

您可以同时使用Linq和XDocument。

将您的文件定义为XDoc,我刚刚将其命名为dDoc

 XDocument Doc = XDocument.Parse(your file/string);

然后定义一个列表,以保存saml字符串中的所有节点,并使用链接过滤NameID

List<XElement> userIDs = (from element in Doc.Descendants()
                                      .Where(x => x.Name.LocalName.Contains("NameID"))
                                      select element).ToList();

然后将所有结果列出到控制台

foreach (XElement element in userIDs)
            {
                Console.WriteLine(element.Value); 
            }