2015-11-13 61 views
1

我試着讀取一個XML文件,但沒有收到只有空格或製表符或新行的節點的內容。請告訴我我錯在哪裏。用cml中的XmlDocument讀取XML文件

XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <paragraph> 
     <sentence> 
      <sequence> 
       <word>aaa</word> 
       <space> </space> 
      </sequence> 
      <sequence> 
       <word>bbb</word> 
       <space> </space> 
      </sequence> 
      <sequence> 
       <word>ccc</word> 
       <space>?!</space> 
      </sequence> 
     </sentence> 
    </paragraph> 
</root> 

的代碼:

XmlDocument doc = new XmlDocument(); 
doc.Load("D:/Licenta/files/struct.xml"); 
XmlNodeList sentences = doc.DocumentElement.SelectNodes("/root/paragraph"); 
foreach (XmlNode sentence in sentences) { 
    Console.WriteLine(sentence.InnerText); 
} 
Console.ReadLine(); 

輸出: aaabbbccc?

+0

你是否在這篇文章中表現了空間性格?http://stackoverflow.com/questions/514635/represent-space-and-tab-in-xml-tag? – erik

+0

現在我嘗試了這個解決方案,它完美的工作。謝謝。 – alexeevyci

回答

2

有上XmlDocument的屬性稱爲PreserveWhitespace默認爲false導致您看到的行爲。在將數據加載到文檔之前,您可能需要考慮將其切換爲true