C# 对XML基本操作代码总结
时间:2022-08-21 16:09:09
具体如下:
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:说明都在注释里。
void TestXML()
{
XmlDocument doc = new XmlDocument();
string path = "https://www.jb51.net/MyDocument/Person.xml";
try
{
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");
XmlElement tagIN = doc.CreateElement("Name");
tagIN.InnerText = "work hard work smart!";
tagOuter.AppendChild(tagIN);
root.AppendChild(tagOuter);//添加tagOuter到XML文件的最后
reader.Close();
doc.Save(path);
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
}
标签:XML,基本操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot日志配置操作全面介绍
2023-03-08 14:37:54
![](https://img.aspxhome.com/file/2023/1/84331_0s.png)
java多线程通过CompletableFuture组装异步计算单元
2023-07-19 10:15:42
![](https://img.aspxhome.com/file/2023/0/57620_0s.jpg)
Java8 如何移除两个相同的List对象
2023-06-07 20:57:50
![](https://img.aspxhome.com/file/2023/9/122259_0s.png)
Android Spinner 下拉菜单的使用
2023-10-25 10:59:53
CCF考试试题之门禁系统java解题代码
2022-10-05 02:39:27
java并发编程专题(六)----浅析(JUC)Semaphore
2023-07-27 23:30:28
面试初级Java开发问到Arrays
2023-11-27 05:40:04
java Spring的启动原理详解
2022-09-02 04:39:59
![](https://img.aspxhome.com/file/2023/4/66244_0s.jpg)
eclipse 如何创建 user library 方法详解
2021-12-29 00:33:56
![](https://img.aspxhome.com/file/2023/7/113417_0s.png)
SrpingDruid数据源加密数据库密码的示例代码
2021-06-21 03:26:26
![](https://img.aspxhome.com/file/2023/1/63021_0s.png)
Java全面细致讲解类与对象
2022-07-07 07:04:42
![](https://img.aspxhome.com/file/2023/6/70936_0s.png)
关于WebView 重定向行为导致的多次加载的问题
2022-09-19 02:45:44
![](https://img.aspxhome.com/file/2023/5/139495_0s.jpg)
C#图像伪彩色处理方法
2022-09-23 10:51:52
![](https://img.aspxhome.com/file/2023/1/92911_0s.jpg)
Java分形绘制山脉模型
2023-05-10 00:51:29
![](https://img.aspxhome.com/file/2023/1/80901_0s.jpg)
C#判断页面中的多个文本框输入值是否有重复的实现方法
2022-10-30 19:41:56
Android可自定义神奇动效的卡片切换视图实例
2022-11-24 09:07:46
![](https://img.aspxhome.com/file/2023/6/97846_0s.gif)
Android保持屏幕常亮2种实现方法
2022-09-08 11:22:43
SpringBoot项目中处理返回json的null值(springboot项目为例)
2023-10-26 04:49:06
Springboot 全局时间格式化操作
2022-08-17 03:23:51
![](https://img.aspxhome.com/file/2023/2/94332_0s.jpg)
c# 应用事务的简单实例
2021-11-24 23:48:34