如何在CSharp中读取文件XML? [英] How Read File XML In CSharp?
本文介绍了如何在CSharp中读取文件XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
用于Exampel
Hi
for Exampel
<pre lang="xml"><?xml version="1.0" encoding="utf-8" ?>
- <ARI>
- <Unit ID="1" Name="آذربایجان شرقی">
<city ID="1">آذرشهر</city>
<city ID="2">اسکو</city>
<city ID="3">اهر</city>
<city ID="4">بستانآباد</city>
<city ID="5">بناب</city>
<city ID="6">تبریز</city>
<city ID="7">جلفا</city>
<city ID="8">چاراویماق</city>
</Unit>
- <Unit ID="2" Name="آذربایجان غربی">
<city ID="1">ارومیه</city>
<city ID="2">اشنویه</city>
<city ID="3">بوکان</city>
<city ID="4">پیرانشهر</city>
<city ID="5">تکاب</city>
<city ID="6">چالدران</city>
<city ID="7">خوی</city>
<city ID="8">خوی</city>
<city ID="9">سردشت</city>
<city ID="10">سلماس</city>
<city ID="11">شاهیندژ</city>
<city ID="12">ماکو</city>
<city ID="13">مهاباد</city>
<city ID="14">میاندوآب</city>
<city ID="15">نقده</city>
</Unit>
</ARI>
1-如何读取此文件并显示ID为1的单位的列表城市?例如
2-如何显示ID = 1的名称和ID单位?
1-how read this file and show list city of unit with id 1 forexample?
2-how show name and id unit with id =1?
推荐答案
以下是要考虑的选项:
Here are the options to consider:
- 使用
System.Xml.XmlDocument
类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
请参见- 使用类
].System.Xml.XmlTextReader
; library/system.xml.xmldocument.aspx"target =" _ blank"title =" New Window> ^ - 使用类
- 使用类
System.Xml.XmlTextReader
;这是最快的读取方法,尤其是您需要跳过一些数据.
请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [
- 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[^].
如果您不限于C#< 3.5.我会使用 LINQ to XML [
If you are not restricted to C# < 3.5. I would use LINQ to XML[^] to that type of query. Very easy to use.
XElement xml = XElement.Load(@"c:\xml\data.xml");
var result = from units in xml.Element("ARI").Elements("Unit")
from city in units.Elements("city")
where city.Attribute("ID").Value == "1"
select city;
您可以使用 ^ ]查询并查找信息.
使用XPath选择节点 [使用XPath和XmlDocument(C#)处理XML数据 [
You can use XPath[^] querys and find info.
Select Nodes Using XPath[^] or Manipulate XML data with XPath and XmlDocument (C#)[^]
这篇关于如何在CSharp中读取文件XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文