2013-10-11 15 views
0

我遇到這個問題,我正在閱讀的XML文件可能有懸掛文本(我不知道它是什麼被稱爲那些沒有被任何標籤包圍的文本)。在XML中,您如何忽略未被標籤包圍的懸掛文本?

例如:

<Book> 
    <Title>The Catcher in the Rye</Title> 
    <Author>Salinger</Author> 
</Book> 
Useless jiberish 
<Book> 
    <Title>Jaws</Title> 
    <Author>Benchley</Author> 
</Book> 

這是除了行 「無用jiberish」 完美XML。發生閱讀沒有任何問題,但是使用的StreamWriter與

XmlWriterSettings settings = new XmlWriterSettings(); 
settings.NewLineOnAttributes = true; 
settings.Indent = true; 

它完美地寫道,直到與「無用jiberish」

<Book> 
    <Title>The Catcher in the Rye</Title> 
    <Author>Salinger</Author> 
</Book>Useless jiberish<Book><Title>Jaws</Title><Author>Bencheley</Author> 

行了。如果我從XML文檔它的工作原理刪除「無用jiberish」寫作完美。但是我沒有這個選擇。有什麼建議/線索爲什麼發生這種情況?這可能是我錯過的非常簡單的事情。

任何建議的幫助。

謝謝。

+0

你正在閱讀或寫作的xml – Anirudha

+0

我正在做兩個。從我看的閱讀中得到正確處理。但是,當我使用MemoryStream將其寫回時,它不能正確寫回。 –

+0

向我們展示了您用於編寫xml的代碼 – Anirudha

回答

0

您可能會考慮使用System.Xml.Linq中的XDocument類。加載文件看起來是這樣的:

 string file = @"c:\test\movies.xml"; 
     XDocument doc = XDocument.Load(file); 
     foreach (XElement e in doc.Root.Descendants()) 
     { 

     } 

更多信息,可以發現有關的XDocument here