繁体   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