2012-03-07 60 views
-1

說我有與XML一個XmlDocument所有的字符串:如何處理在一個XmlDocument

<office:body> 
    <text:p>One</text:p> 
    <text:p>two 
    <text:span>three 
     <text:line-break/>four 
     <text:line-break/>five 
     <text:line-break/>six 
    </text:span>seven 
    </text:p> 
    <text:p>eight</text:p> 
    <table:table> 
    <table:table-column> 
    <table:table-column> 
    <table:table-row> 
     <table:table-cell> 
     <text:p> 
      <text:span>nine</text:span> 
     </text:p> 
     </table:table-cell> 
     <table:table-cell> 
     <text:p> 
      <text:span>ten</text:span> 
     </text:p> 
     </table:table-cell> 
    </table:table-row> 
    </table:table> 
</office:body> 

我怎麼能只提取的話,一次一個?

回答

0

您可以用XMLReader,這樣做:

class Program { 

    static void Main() { 
     XmlReader reader = new XmlTextReader(new StringReader(Xml)); 
     while (reader.Read()) { 
      if (reader.NodeType == XmlNodeType.Text && reader.HasValue) { 
       Console.WriteLine(reader.Value.Trim()); 
      } 
     } 
    } 

    private const string Xml = @" 
     <root xmlns:office=""http://office"" 
      xmlns:text=""http://text"" 
      xmlns:table=""http://table""> 
     <office:body> 
      <text:p>One</text:p> 
      <text:p>two 
      <text:span>three 
       <text:line-break/>four 
       <text:line-break/>five 
       <text:line-break/>six 
      </text:span>seven 
      </text:p> 
      <text:p>eight</text:p> 
      <table:table> 
      <table:table-column/> 
      <table:table-column/> 
      <table:table-row> 
       <table:table-cell> 
       <text:p> 
        <text:span>nine</text:span> 
       </text:p> 
       </table:table-cell> 
       <table:table-cell> 
       <text:p> 
        <text:span>ten</text:span> 
       </text:p> 
       </table:table-cell> 
      </table:table-row> 
      </table:table> 
     </office:body> 
     </root>"; 
} 

您需要添加一個根標籤來處理XML命名空間,並修正XML的規範格式錯誤這個工作。

+0

你指的是什麼樣的良構錯誤? – matthewk 2012-03-08 19:25:40

+0

@matthewk您的XML在兩個''標記中缺少結束斜線。 – dasblinkenlight 2012-03-08 19:32:42

+0

謝謝。不過,這種解決方案不會分別得到「二」,「三」,「四」,「五」,「六」和「七」。 – matthewk 2012-03-08 20:10:33