简体   繁体   中英

Extract an specific node from XML file in C#

I am trying to extract an specific node from an XML file using C#. I would like to extract just the userID (123456789) from this XML

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

This is my code:

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

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

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

OUTPUT:

UserID: System.Xml.XmlElementList

I would like to get something like this:

User ID: 123456789

Using 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();
         }
    }


}

You can use both Linq and XDocument.

Define your file as XDoc, I've just named it dDoc

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

Then define a list to hold all of the nodes in the saml string, and use link to filter for NameID

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

then list all of the results to console

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM