从VB.NET中的XML文件中提取特定节点

时间:2019-08-01 20:09:28

标签: xml vb.net visual-studio

我正在尝试使用VB.NET从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>

我已经使用C#做到了:

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

我需要同样的东西,但是使用VB.NET

我想得到这样的东西:

User ID: 123456789

1 个答案:

答案 0 :(得分:1)

您可以直接翻译C#方式:

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

但是使用VB方式更有趣:

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