C# 对XML基本操作总结
2016-04-07 19:46
543 查看
C# 对XML基本操作包括读取节点的数据,添加节点。读取节点属性,修改节点属性等。具体如下:
XML文件:文件在MyDocument文件夹下
Code:说明都在注释里。
下文是对C#对XML的具体操作
如何完成.Net下XML文档的读写操作
C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
C#中用SelectSingleNode方法解析带有多个命名空间的XML文件
原文链接:http://www.cnblogs.com/linlf03/archive/2011/10/10/2205896.html
XML文件:文件在MyDocument文件夹下
<?xml version="1.0" encoding="utf-8"?> <PersonF xmlns="" Name="(test)work hard work smart!"> <person Name="Person1"> <ID>1</ID> <Name>XiaoA</Name> <Age>59</Age> </person> <person Name="Person2"> <ID>2</ID> <Name>XiaoB</Name> <Age>29</Age> </person> <person Name="Person3"> <ID>3</ID> <Name>XiaoC</Name> <Age>103</Age> </person> <person Name="Person4"> <ID>4</ID> <Name>XiaoD</Name> <Age>59</Age> </person> </PersonF>
Code:说明都在注释里。
public void TestXML() { XmlDocument doc = new XmlDocument(); string path = "http://www.cnblogs.com/MyDocument/Person.xml"; try { //doc.Load(Server.MapPath() doc.Load(path); //1、读取单个节点的数据 XmlNode node = doc.SelectSingleNode("PersonF"); //2、读取多个节点的数据 XmlNodeList nodeList1 = doc.SelectNodes("PersonF/person"); //3.1 读取具体节点的具体值 如:属性为Person2的第二个节点Name的InnerText XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("person"); foreach (XmlNode node2 in nodeList1) //当然也能用nodeList的值 { if (node2.Attributes["Name"].InnerText == "Person2") { Console.WriteLine(node2.ChildNodes[1].InnerText); } } //3.2 读取ID为2所在的节点第二个子节点Name的InnerText XmlNode node3 = doc.SelectSingleNode("PersonF/person[ID=2]"); string strNode3 = node3.ChildNodes[1].InnerText; //3.3利用下面的方法可以找到ID为2的节点 XmlNodeList nodeList2 = doc.SelectNodes("//person//ID"); XmlNode node4 = null; foreach (XmlNode node5 in nodeList2) { if (node5.InnerText == "2") { node4 = node5; break; } } Console.WriteLine(node4.InnerText); //4、读取节点的属性 string Name = node.Attributes["Name"].InnerText; //5 修改节点的属性 node.Attributes["Name"].InnerText = "work hard work smart!"; doc.Save(path); //6 添加自定义的节点 XmlTextReader reader = new XmlTextReader(path); XmlElement root = doc.DocumentElement;//获取根节点 XmlElement tagOuter = doc.CreateElement("person"); //设置节点属性 tagOuter.SetAttribute("Name", "Person5"); XmlElement tagIN_Name = doc.CreateElement("Name"); XmlElement tagIN_ID = doc.CreateElement("ID"); tagIN_Name.InnerText = "work hard work smart!"; tagIN_ID.InnerText = "32"; tagOuter.AppendChild(tagIN_Name); tagOuter.AppendChild(tagIN_ID); root.AppendChild(tagOuter);//添加tagOuter到XML文件的最后 reader.Close(); doc.Save(path); } catch (System.Exception e) { throw new Exception(e.Message); } }
下文是对C#对XML的具体操作
如何完成.Net下XML文档的读写操作
C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
C#中用SelectSingleNode方法解析带有多个命名空间的XML文件
原文链接:http://www.cnblogs.com/linlf03/archive/2011/10/10/2205896.html
相关文章推荐
- c# 文本框纪录快捷键并处理冲突的系统热键
- C#语言中提供了枚举和整型之间的强制转换功能(SelectItem)
- c#中datagrid控件的基本使用
- C# winfrom 长时间检查不到操作,自动关闭应用程序
- c# 正则表达式如何处理换行符?
- C# 测试代码段性能耗时
- AE+C# 版本更新问题 命名空间“ESRI”中不存在类型或命名空间名称“Arcgis”(是缺少程序集引用吗?)
- C# 反射机制
- 从OC和C#中找乐趣:相同又不同的delegate
- C#中将DataTable转成List
- HP-Socket国产优秀socket通信组件推荐
- C#list泛型的定义,作用,用法
- C# Hadoop
- C#中将数据的格式显示为千分位显示格式
- C# 中资源文件的类型
- C#学习---C#简介
- c# 嵌入的资源
- C#控制台程序中处理2个关闭事件的代码实例
- C#分隔符
- C#未将对象引用设置到对象的事例原因及解决办法