0

I am trying to extract an specific node from an XML file using VB.NET. 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>

I already did it using C#:

string UserID = (string)Doc.Descendants().Where(x => x.Name.LocalName == "NameID").FirstOrDefault();

I need the same but using VB.NET

I would like to get something like this:

User ID: 123456789

1 Answer 1

2

You can directly translate the C# way:

Dim UserID As String = CType(Doc.Descendants().Where(Function(x) x.Name.LocalName = "NameID").FirstOrDefault(), String)

But it's much more fun to use the VB way:

Imports <xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">  'Declare the namespace prefix

Module Module1
    Sub Main()
        Dim Doc = <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:Assertion>
                  </Response>

        Dim UserID As String = Doc...<saml:NameID>.Value  'Use the prefix

        Console.WriteLine(UserID)
        Console.ReadLine()

    End Sub

End Module
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.