【问题标题】:How to parse a xml and read its values如何解析 xml 并读取其值
【发布时间】:2013-06-13 03:01:54
【问题描述】:

我正在使用 C# windows phone 8,我有以下 XML

<?xml version="1.0" encoding="UTF-8" ?> 
 <login res="SUCCESS"  encstatus="DEFAULT" usedquota="0"  />

我需要提取 res、encstatus 和 usedQuota 的值。

如何在 xml 解析中做到这一点?

我试过了

 XDocument xDoc = XDocument.Parse(str);
            var pol = xDoc.Element("res");
            var items = xDoc.Descendants("res");

其中str为xml文件,但所有元素均为空/null。

【问题讨论】:

  • 你想要的是属性,而不是元素
  • 是的,我可以通过这种方式获取属性值。实际上这是来自服务器的响应。

标签: c# xml-parsing windows-phone-8


【解决方案1】:

您正在尝试获取 属性 值,没有元素:

XDocument xDoc = XDocument.Parse(str);

var pol = (string)xDoc.Root.Attribute("res");

【讨论】:

    【解决方案2】:

    那些节点是属性:

     XDocument xDoc = XDocument.Parse(str)
     XElement login = xDoc.Root;
     string res = (string)login.Attribute("res");
     string encstatus = (string)login.Attribute("encstatus");
     int usedquota = (int)login.Attribute("usedquota");
    

    【讨论】:

    • 谢谢...你帮了我很多。没想到这么简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2011-04-01
    • 2012-09-11
    • 2023-04-03
    • 2018-05-14
    • 1970-01-01
    相关资源
    最近更新 更多