簡體   English   中英

在其他文本中間解析XML的最佳方法是什么

[英]What's the best way to parse XML in the middle of other text

如何解析其他文本中的xml。

示例:如果我在C#中有此文本文件,如何解析xml部分:

-> Begin of file

2010-01-01 tehgvdhjjsad  
2010-01-02 dsjhnxcucncu  
14:55 iahsdahksdjh  

<Answer>
<headline>
<a1>1</a1>
<a2>2</a2>
</headline>
</Answer>
2010-01-05 tehgvddsda  
2010-01-05 ddsada  
22:55 iahsdahksdjh2  

<Answer>
<headline>
<a1>11</a1>
<a2>22</a2>
</headline>
</Answer>
-> End of file

幾種方法:

 1. Do a string.IndexOf("<Answer>") and then use a substring to chop off the header information.  Then add the substring like this:
xmlString = "<Answers>" + substringXml + "</Answers>".  Then you could parse the xml as valid XML.
 2. Use an xmltextreader created with fragment conformance levels and read through the xml.  Only stop on the Answer elements and do processing.
 3. Add a root element to the document and open it in an XmlDocument and use an xpath expression to read out the Answer elements.

好吧,沒有什么可以幫助您完成某些事情的事情。 AFAIK有兩種可能性:

選項1.如果所有xml片段都具有相同的根節點,即。 “ <Answer>”,那么您可以簡單地通過<Answer>的出現查找循環,找到下一個關閉</ Answer>的出現,提取兩者之間的文本並使用常規的XML解析器。

選項2。如果這是xml的事情,那么您可以使用我前一段時間編寫的基於Regex的Html Parser 它應該毫無問題地處理這些輸入。 但是,您將不得不處理打開/關閉元素並確定如何處理它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM