【问题标题】:How to parse Xml containing xsi in windows phone 8?如何在 Windows Phone 8 中解析包含 xsi 的 Xml?
【发布时间】:2014-05-05 15:29:52
【问题描述】:

我有一个要从独立存储打开的文件。我得到一个字符串类型的 XML。 我需要解析 XML 以获取特定的字段内容,但遇到一些问题。

我尝试使用XDocument.Parse 并获得了XML,然后我使用Descendants 并写了我需要的节点的名称,但是在调试它时 - 我看到返回值为NULL。

谁能告诉我我做错了什么?

IsolatedStorageFileStream file = storage.OpenFile(txtFilePath, FileMode.Open, FileAccess.Read);
                using (StreamReader reader = new StreamReader(file))
                   {
                     //message += reader.ReadToEnd();
                      message = reader.ReadToEnd();
                   }
                  var doc = XDocument.Parse(message);
                  var res = doc.Descendants("value").Select(o => o.Value).ToArray();
                  // res is NULL
                  var x = res[0];
                  message = x;

这是XML文档,我只需要<value>节点下的ine(第三行)

    <xsi:document xmlns="@link" xmlns:xsi="@link" xsi:schemaLocation="@link" version="1.0">
        <xsi:field left="51" top="235" right="224" bottom="280" type="text">
        <xsi:value encoding="utf-16">| amount: 152.467</xsi:value> --- THE LINE I NEED
        <xsi:line left="52" top="245" right="179" bottom="267">
        <xsi:char left="52" top="245" right="55" bottom="267" confidence="93">|</xsi:char>
        <xsi:char left="56" top="245" right="78" bottom="267" confidence="100"></xsi:char>
        <xsi:char left="79" top="254" right="84" bottom="261" confidence="43">a</xsi:char>
        <xsi:char left="86" top="254" right="96" bottom="261" confidence="100">m</xsi:char>
        <xsi:char left="98" top="254" right="105" bottom="261" confidence="94">o</xsi:char>
        <xsi:char left="106" top="254" right="112" bottom="261" confidence="31">u</xsi:char>
        <xsi:char left="114" top="254" right="120" bottom="261" confidence="34">n</xsi:char>
        <xsi:char left="121" top="252" right="126" bottom="261" confidence="59">t</xsi:char>
        <xsi:char left="127" top="254" right="129" bottom="261" confidence="76">:</xsi:char>
        <xsi:char left="130" top="252" right="133" bottom="261" confidence="100"></xsi:char>
        <xsi:char left="134" top="252" right="140" bottom="261" confidence="100">1</xsi:char>
         <xsi:char left="141" top="252" right="147" bottom="261" confidence="100">5</xsi:char>
        <xsi:char left="148" top="252" right="154" bottom="261" confidence="55">2</xsi:char>
        <xsi:char left="155" top="259" right="157" bottom="261" confidence="100" suspicious="true">.</xsi:char>
        <xsi:char left="158" top="252" right="165" bottom="261" confidence="71">4</xsi:char>
        <xsi:char left="166" top="252" right="172" bottom="261" confidence="40">6</xsi:char>
        <xsi:char left="173" top="252" right="179" bottom="261" confidence="100">7</xsi:char>
       </xsi:line>
     </xsi:field>
   </xsi:document>

非常感谢,

【问题讨论】:

  • 如果我昨天没记错的话,你已经发布了这个问题......并得到了答案。

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


【解决方案1】:

由于您的 XML 使用命名空间,您需要将命名空间指定给 Descendants 方法。 xsi 命名空间由 XML 文档开头的属性 xmlns:xsi="@link" 定义。

所以在你的情况下,代码是:

var doc = XDocument.Parse(message);
var res = doc.Descendants(XName.Get("value", "@link")).Select(o => o.Value).ToArray();
var x = res[0];

【讨论】:

    猜你喜欢
    • 2015-10-18
    • 2013-05-02
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多