用于读取下面给出的XML文件的C#代码...想要读取节点值,如11111 for< ns0:Cell ns0:tableColumnName ="数字(带)“ NS0:colNo = QUOT; 2英寸;> 11111< / NS0:电池> [英] C# code to read the XML file given below... Want to read the node values like 11111 for <ns0:Cell ns0:tableColumnName=" number (with )" ns0:colNo="2">11111</ns0:Cell>
本文介绍了用于读取下面给出的XML文件的C#代码...想要读取节点值,如11111 for< ns0:Cell ns0:tableColumnName ="数字(带)“ NS0:colNo = QUOT; 2英寸;> 11111< / NS0:电池>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< ns0:Root xmlns =urn:x-srid_data:namespacexmlns:ns0 =urn:x-srid_data:namespacens0:versionXML =1.0>
< ns0:DOCUMENT ns0:name =C:\ Users \ddd \Desktop \ddd.dotx>
< ns0:表ns0:name =化合物编号(带盐码)| ns0:tableNo =1ns0:WordTableGUID =a4133206-89eb-4771-b8bc-787011b742b9>
< ns0:ROWS>
< ns0:ROW ns0:rowNumber =1>
< ns0:Cell ns0:tableColumnName =Unknownns0:colNo =1> Compound(with)< / ns0:Cell>
< ns0:Cell ns0:tableColumnName =number(with)ns0:colNo =2> 11111< / ns0:Cell>
< / ns0:ROW>
< ns0:ROW ns0:rowNumber =2>
< ns0:Cell ns0:tableColumnName =number(with)ns0:colNo =1>新数字< / ns0:Cell>
< ns0:Cell ns0:tableColumnName =New numberns0:colNo =2> NEW NUMBER< / ns0:Cell>
< / ns0:ROW>
< ns0:ROW ns0:rowNumber =3>
< ns0:Cell ns0:tableColumnName =number(with)ns0:colNo =1>上一个数字< / ns0:Cell>
< ns0:Cell ns0:tableColumnName =Previous numberns0:colNo =2> PREVIOUS NUMBER< / ns0:Cell>
< / ns0:ROW>
< / ns0:ROWS>
< / ns0:TABLE>
< / ns0:DOCUMENT>
< / ns0:Root>
解决方案
您可以使用.NET FCL中提供的不同工具解析XML。这是我对他们的简短概述:
- 使用
System.Xml.XmlDocument
class 。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。- 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。- 使用类
System.Xml.Linq.XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。
-SA
您可以使用Linq查询查找节点值并获取XML的所有值
<ns0:Root xmlns="urn:x-srid_data:namespace" xmlns:ns0="urn:x-srid_data:namespace" ns0:versionXML="1.0"> <ns0:DOCUMENT ns0:name="C:\Users\ddd\Desktop\ddd.dotx"> <ns0:TABLE ns0:name="Compound number (with salt code)|" ns0:tableNo="1" ns0:WordTableGUID="a4133206-89eb-4771-b8bc-787011b742b9"> <ns0:ROWS> <ns0:ROW ns0:rowNumber="1"> <ns0:Cell ns0:tableColumnName="Unknown" ns0:colNo="1">Compound (with )</ns0:Cell> <ns0:Cell ns0:tableColumnName=" number (with )" ns0:colNo="2">11111</ns0:Cell> </ns0:ROW> <ns0:ROW ns0:rowNumber="2"> <ns0:Cell ns0:tableColumnName=" number (with )" ns0:colNo="1">New number</ns0:Cell> <ns0:Cell ns0:tableColumnName="New number" ns0:colNo="2">NEW NUMBER</ns0:Cell> </ns0:ROW> <ns0:ROW ns0:rowNumber="3"> <ns0:Cell ns0:tableColumnName=" number (with )" ns0:colNo="1">Previous number</ns0:Cell> <ns0:Cell ns0:tableColumnName="Previous number" ns0:colNo="2">PREVIOUS NUMBER</ns0:Cell> </ns0:ROW> </ns0:ROWS> </ns0:TABLE> </ns0:DOCUMENT> </ns0:Root>解决方案You can parse XML using different facilities available in .NET FCL. This is my short overview of them:
- 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[^].
—SA
You can use Linq query to find the node value and get all values of XML
这篇关于用于读取下面给出的XML文件的C#代码...想要读取节点值,如11111 for< ns0:Cell ns0:tableColumnName ="数字(带)“ NS0:colNo = QUOT; 2英寸;> 11111< / NS0:电池>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文