本文介绍了如何从字符串中只提取xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<processresponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><extensiondata /><errorcode>0</errorcode><errormessage /><message>successful.</message></processresponse>
i只需提取..
i have to extract only..
<errorcode>0</errorcode><errormessage /><message>successful.</message>
推荐答案
- 使用
System.Xml.XmlDocument
class 。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 []。 - 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 []。 - 使用类
System.Xml.Linq.XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 [], []。
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]. - Use the class
System.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^]. - Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
string str = @"<processresponse xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><extensiondata /><errorcode>0</errorcode><errormessage /><message>successful.</message></processresponse>";
Regex reg = new Regex(@"<errorcode>.*<\/message>");
Match tmp = reg.Match(str);
Console.WriteLine(tmp.Value);
这篇关于如何从字符串中只提取xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!