C#读取XML数据
2009-01-12 09:49
288 查看
最近刚学C#,很多地方不会,边学边写了一个类,用于读取xml文件里的数据。
xml文件定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<path id="1">c:\windows</path>
</configuration>
下面是读取xml文件的类:
此类的getPath()方法用于通过id值和path子元素,获得相应的元素值。
xml文件定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<path id="1">c:\windows</path>
</configuration>
下面是读取xml文件的类:
using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace xml { class readXML { private string filename; //文件路径 public string str=null; //构造函数,filename是xml文件的路径 public readXML(string filename) { this.filename = filename; } public string getPath(string id, string str) { XmlTextReader reader = new XmlTextReader(filename); while (reader.Read()) { if (reader.Name == str && reader.GetAttribute("id") == id) { str = reader.ReadString(); reader.Close(); return str; } } reader.Close(); return null; } }
此类的getPath()方法用于通过id值和path子元素,获得相应的元素值。
相关文章推荐
- C# 编写代码生成XML文件,并从XML中读取出数据
- [转] C#下 读取xml节点的数据总结
- C# XML操作之读取XML数据
- C#下 读取xml节点的数据总结 .txt
- C#利用XmlTextReader读取XML节点数据
- C# 读取xml数据和vaule
- c# 读取XML数据
- C#递归读取XML菜单数据的方法
- C#下 读取xml节点的数据总结
- C#下 读取xml节点的数据总结 .txt
- C#读取URL里面的XML数据问题
- C# 自定义数据类型 数组应用 读取XML
- C#读取XML文件数据和把数据保存至xml的方法
- C#中创建与读取XML数据(一)
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- C#读取xml节点数据方法小结
- C#下读取xml节点的数据
- C#读取网页xml数据
- 读取XML示例:C#获取XML的数据
- C#不使用DataSet操作XML,XmlDocument读写xml所有节点及读取xml节点的数据总结