xml解析并将值转换为字符串C#

时间:2016-02-17 13:56:08

标签: c# xml

我有像这样的XML格式

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<abresponse IssuerId="5" ReturnCode="Success" Version="12.2">
    <XMLAuthenticateResponse>
        <ACL>
            <ACL Name="PM.ADMIN" Value="1" /> 
            <ACL Name="PM.APPROVE" Value="1" /> 
            <ACL Name="PM.LOGIN" Value="1" /> 
            <ACL Name="PM.SUPPORT" Value="1" /> 
        </ACL>
        <User EMailAddress="abc@xyz.com" GroupName="Program Administrator" UserId="2095965" Username="test" /> 
    </XMLAuthenticateResponse>
</abresponse >

如何从XML中获取ReturnCode ..

我将此作为API响应,因此我已将其转换为

XElement response = XElement.Parse(xDoc.OuterXml)
string retcode=response.Descendants("OrbiscomResponse")
                       .Attributes("ReturnCode")
                       .FirstOrDefault()
                       .Value
                       .Tostring();

1 个答案:

答案 0 :(得分:0)

只需删除后代调用即可正常工作:

string retcode = response
           .Attributes("ReturnCode")
           .FirstOrDefault()
           .Value
           .ToString();