【问题标题】:Parsing XML in Windows Phone 7在 Windows Phone 7 中解析 XML
【发布时间】:2012-01-22 01:20:23
【问题描述】:
<A xmlns="http://www.aaa.com/bbb/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">    
<B>
  <aa>AUH</aa><bb>5</bb><cc>Abu</cc>
</B>
<B>
  <aa>AUH</aa><bb>7</bb><cc>ghi</cc>
</B>
  <ServiceResponse><ErrorMessage i:nil="true"/><ExecutionStatus>SUCCESS</ExecutionStatus></ServiceResponse>
</A>

我有一个类似上面的 XML,我已经为 A、B 和 ServiceResponse 创建了类。

我正在尝试使用以下代码进行解析:

XmlSerializer ser = new XmlSerializer(typeof(A));
objAirportListResponse = ser.Deserialize(new StringReader(str)) as A;

在上面的代码中,“str”是包含上面 XML 的字符串。

我收到如下错误:

XML 文档中存在错误 (1, 2)(System.InvalidOperationException)

如果我删除 xmlns="http://www.aaa.com/bbb/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 从 XML 我得到了同样的错误 因为 i:nil="true"。然后我删除了 xmlns="http://www.aaa.com/bbb/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 和 i:nil="true" 我试过了解析XML,这次成功解析到class。

但删除 xmlns="http://www.aaa.com/bbb/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 和 i:nil="true"不是正确的方法,请帮助我如何解析上面的示例。

提前致谢。

【问题讨论】:

标签: xml windows-phone-7


【解决方案1】:

我同意上述 cmets 中 Will 关于 Linq-to-XML 方法更可靠的观点,但此链接可能对您有用:

Use multiple namespaces when deserializing with .NET XmlSerializer

在反序列化它以获取要读入的数据之前,您必须使用 Xml 属性对您的类进行属性化。我建议以另一种方式工作一段时间 - 尝试一遍又一遍地将您的类序列化为 XML,并在此过程中进行调整直到你得到你想要读入的准确输出,然后尝试读入“真实”数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2012-11-15
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多