2

Please how can I get value of attribute Value of element StatusCode in this XML:

<LogoutResponse 
    ID="_f525259e-7e91-4282-9dc3-a0da65a4a17a" 
    Version="2.0" 
    IssueInstant="2021-05-17T15:41:55Z" 
    InResponseTo="_5089729f-5cc0-4a66-a3c1-e710cde92897" 
    Destination="https://idp.xyz/logout.aspx" 
    xmlns="urn:oasis:names:tc:SAML:2.0:protocol">
    <Issuer xmlns="urn:oasis:names:tc:SAML:2.0:assertion">https://abc.xyz/api</Issuer>
    <Status>
        <StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </Status>
</LogoutResponse>
Pavel Hodek
  • 14,319
  • 3
  • 32
  • 37
  • There are multiple APIs for processing XML; which are you using? what have you tried? Have you looked at `XDocument`, for example? or `XmlDocument` (which is a completely separate API to do the same thing), or `XmlSerializer`? – Marc Gravell May 18 '21 at 08:13
  • Does this answer your question? [Getting attribute using XPath](https://stackoverflow.com/questions/4531995/getting-attribute-using-xpath) – Palle Due May 18 '21 at 08:15
  • @MarcGravell Anything `XDocument` or `XmlDocument`. But there is something with namespaces and I do not know, how to write code properly. Please see that @Krishna Muppalla code is now working and I donť know why. – Pavel Hodek May 18 '21 at 08:35

3 Answers3

2

Due to the namespaces, this gets a little messy:

var doc = new XmlDocument();
doc.LoadXml(@"<LogoutResponse 
    ID=""_f525259e-7e91-4282-9dc3-a0da65a4a17a""
    Version=""2.0"" 
    IssueInstant=""2021-05-17T15:41:55Z"" 
    InResponseTo=""_5089729f-5cc0-4a66-a3c1-e710cde92897""
    Destination=""https://idp.xyz/logout.aspx"" 
    xmlns=""urn:oasis:names:tc:SAML:2.0:protocol"">
    <Issuer xmlns=""urn:oasis:names:tc:SAML:2.0:assertion"">https://abc.xyz/api</Issuer>
    <Status>
        <StatusCode Value=""urn:oasis:names:tc:SAML:2.0:status:Success"" />
    </Status>
</LogoutResponse>");

var ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("oasis", "urn:oasis:names:tc:SAML:2.0:protocol");
var attr = (XmlAttribute)doc.SelectSingleNode(
    "/oasis:LogoutResponse/oasis:Status/oasis:StatusCode/@Value", ns);
System.Console.WriteLine(attr.Value);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

You can try with XmlDocument.

Note: since you have a name space (xmlns ...) all elements in your xml by default in name space. hence you are getting object reference error. I have updated the code and .netfiddle. please have a look

var xml = @"<LogoutResponse 
ID=""_f525259e-7e91-4282-9dc3-a0da65a4a17a"" 
Version=""2.0"" 
IssueInstant=""2021-05-17T15:41:55Z"" 
InResponseTo=""_5089729f-5cc0-4a66-a3c1-e710cde92897"" 
Destination=""https://idp.xyz/logout.aspx"" 
xmlns=""urn:oasis:names:tc:SAML:2.0:protocol"">
<Issuer xmlns=""urn:oasis:names:tc:SAML:2.0:assertion"">https://abc.xyz/api</Issuer>
<Status>
<StatusCode Value=""urn:oasis:names:tc:SAML:2.0:status:Success"" />
</Status>
</LogoutResponse>";

var doc = new System.Xml.XmlDocument();                
doc.LoadXml(xml);

var nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("ns", "urn:oasis:names:tc:SAML:2.0:protocol");

var result = doc.SelectSingleNode("/ns:LogoutResponse/ns:Status/ns:StatusCode/@Value", nsManager).InnerText;
Console.WriteLine(result);
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
0

By using LINQ to XML API.

It is available in the .Net Framework since 2007.

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<LogoutResponse xmlns='urn:oasis:names:tc:SAML:2.0:protocol' 
        Destination='https://idp.xyz/logout.aspx' ID='_f525259e-7e91-4282-9dc3-a0da65a4a17a' 
        InResponseTo='_5089729f-5cc0-4a66-a3c1-e710cde92897' IssueInstant='2021-05-17T15:41:55Z' Version='2.0'>
        <Issuer xmlns='urn:oasis:names:tc:SAML:2.0:assertion'>https://abc.xyz/api</Issuer>
        <Status>
            <StatusCode Value='urn:oasis:names:tc:SAML:2.0:status:Success'></StatusCode>
        </Status>
    </LogoutResponse>");

    XNamespace ns1 = "urn:oasis:names:tc:SAML:2.0:protocol";

    string StatusCodeValue = xdoc.Descendants(ns1 + "StatusCode")
        .Attributes("Value")
        .FirstOrDefault()?.Value;

    Console.WriteLine("StatusCodeValue='{0}'", StatusCodeValue);
}

Output

StatusCodeValue='urn:oasis:names:tc:SAML:2.0:status:Success'
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21