Search code examples
c#.netxmlnodessaml

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

Solution

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