C# 什么';是在其他文本中间解析XML的最好方法

C# 什么';是在其他文本中间解析XML的最好方法,c#,xml,parsing,C#,Xml,Parsing,如何在其他文本的中间解析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>

如何在其他文本的中间解析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
->文件开头
2010-01-01 TEHGVDHJSAD
2010-01-02 dsjhnxcucncu
14时55分
1.
2.
2010-01-05 tehgvddsda
2010-01-05 DD萨达
22:55 iahsdahksdjh2
11
22
->文件结束
有几种方法:

 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.
1。执行string.IndexOf(“”),然后使用子字符串切掉标题信息。然后像这样添加子字符串:
xmlString=”“+子字符串xml+“”。然后可以将xml解析为有效的xml。
2.使用使用片段一致性级别创建的xmltextreader并读取xml。仅停止回答元素并进行处理。
3.将根元素添加到文档中,并在XmlDocument中打开它,然后使用xpath表达式读取答案元素。

好吧,没有太多东西可以帮助你解决一些问题。AFAIK有两种可能性:


备选案文1。如果所有xml片段都具有相同的根节点,即。“我不久前写过。它应该毫无疑问地处理这些输入;但是,您必须处理打开/关闭元素,并确定如何处理它们。

我甚至不会尝试这样做。最好的方法是指定有效的文件格式。XML中没有的另一个crud可以很容易地放在节点或CDATA部分的XML中。指定有效的输入格式是完全可以接受和有效的做法。您的“其他文本”是什么?它是否可以包含XML元字符,比如&或者顺便说一句,我认为选项2和3可能会遇到诸如