2014-03-26 49 views
0

我有一個文件,我從隔離存儲打開。我得到一個字符串類型的XML。 我需要解析XML來獲取特定的字段內容,但有關於它的一些問題。XDocument.Parse在c#WP8中返回NULL後面的命令

我試過使用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> 
     <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> 

回答

1

的問題是命名空間XSI。首先,聲明它

XNamespace xsi = "..."; 

,然後改變你的LINQ查詢

var res = doc.Descendants(xsi+"value").Select(o => o.Value).ToArray(); 
+0

嘿伊戈爾,但我的節點看起來像這樣: |金額:152.467。我應該在xsi =「...」中寫些什麼嗎? – user1386966

+1

嗯..我不認爲這個答案值得downvote – har07

+1

@ user1386966「...」是一個佔位符,使用一個真正的命名空間(「@link」) –

0

除了@伊戈爾的答案,你需要把URI指向的XML命名空間:

xmlns:xsi="@link" 

XNamespace變量的值:

XNamespace xsi = "@link"; 
var res = doc.Descendants(xsi+"value").Select(o => o.Value).ToArray(); 
+0

非常感謝!有效 – user1386966