【问题标题】:xml parsing and convert values to string C#xml解析并将值转换为字符串C#
【发布时间】:2016-02-17 13:56:08
【问题描述】:

我有这样的 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();

【问题讨论】:

  • 发布您当前的代码并解释您遇到的问题,以便我们帮助解决它。
  • 向我们展示你的努力
  • 可以将xml加载为string,找到ReturnCode=并获取值。理论上。
  • 试试XmlElement.GetAttribute Method
  • 您的 XML 有 abresponse 但您的代码正在寻找 OrbiscomResponse 还有什么是 xDoc?您不需要解析 xml 两次。

标签: c# xml


【解决方案1】:

只需删除 Descendants 调用,它应该可以正常工作:

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

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多