XDocument.Parse 后的后代命令在 c# WP8 中返回 NULL [英] Descendant command after XDocument.Parse returns NULL in c# WP8
问题描述
我有一个要从独立存储中打开的文件.我得到一个字符串类型的 XML.我需要解析 XML 以获取特定的字段内容,但在这方面存在一些问题.
I have a file that I'm opening from the Isolated storage. I'm getting an XML that is string type. I need to parse the XML to get specific field content but having some problems regarding it.
我尝试使用 XDocument.Parse
并获得 XML,然后我使用 Descendants
并写下我需要的节点的名称,但是在调试时 -我看到返回值为NULL.
I've tried to use XDocument.Parse
and got the XML, then I used Descendants
and wrote the name of the node I needed , but when debugging it - I see that the returned value is NULL.
谁能告诉我我做错了什么?
Can someone please tell me what am I doing wrong?
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文档,我需要的唯一内容是
节点下的ine(第三行)
This is the XML document, The only content I need is the ine under the node <value>
(third line)
<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>
<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>
推荐答案
问题是命名空间 xsi.首先声明一下
The problem is the namespace xsi. First, declare it
XNamespace xsi = "...";
然后将您的 LINQ 查询更改为
and then change your LINQ query to
var res = doc.Descendants(xsi+"value").Select(o => o.Value).ToArray();
这篇关于XDocument.Parse 后的后代命令在 c# WP8 中返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!