C#操作Xml节点

见过不少人、经过不少事、也吃过不少苦,感悟世事无常、人心多变,靠着回忆将往事串珠成链,聊聊感情、谈谈发展,我慢慢写、你一点一点看......

1、增加节点

复制代码
public static bool AppendChild(string filePath, string xPath, XmlNode xmlNode){
  
  	try	{
  
  		XmlDocument doc = new XmlDocument();		doc.Load(filePath);		XmlNode xn = doc.SelectSingleNode(xPath);		XmlNode n = doc.ImportNode(xmlNode, true);		xn.AppendChild(n);		doc.Save(filePath);		return true;	}	catch	{
  
  		return false;	}}

2、修改节点

复制代码
public static bool UpdateNodeInnerText(string filePath, string xPath, string value){
  
  	try	{
  
  		XmlDocument doc = new XmlDocument();		doc.Load(filePath);		XmlNode xn = doc.SelectSingleNode(xPath);		XmlElement xe = (XmlElement)xn;		xe.InnerText = value;		doc.Save(filePath);	}	catch	{
  
  		return false;	}	return true;}

3、查询节点

复制代码
public static XmlNodeList ReadNodes(string filePath, string xPath){
  
  	try	{
  
  		XmlDocument doc = new XmlDocument();		doc.Load(filePath);		XmlNode xn = doc.SelectSingleNode(xPath);		XmlNodeList xnList = xn.ChildNodes;  		return xnList;	}	catch	{
  
  		return null;	}}

关注我,不失联。有啥问题请留言。

感情恋爱合集https://blog.csdn.net/forever8341/category_12863789.html

职业发展故事https://blog.csdn.net/forever8341/category_12863790.html

常用代码片段https://blog.csdn.net/forever8341/category_12863793.html

程序开发教程https://blog.csdn.net/forever8341/category_12863792.html

自我备考经验 https://blog.csdn.net/forever8341/category_12863791.html

高阶高效代码https://blog.csdn.net/forever8341/category_12873345.html

金融语言解析https://blog.csdn.net/forever8341/category_12877262.html

相关推荐
l1t1 小时前
我改写的二分法XML转CSV文件程序速度追上了张泽鹏先生的
xml·c语言·人工智能·算法·expat
困到晕阙13 小时前
[NCTF2019]Fake XML cookbook
xml·xxe
ruleslol15 小时前
五分钟XML速成
xml
xiaowu08017 小时前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
VisionPowerful19 小时前
九.弗洛伊德(Floyd)算法
算法·c#
ArabySide19 小时前
【C#】 资源共享和实例管理:静态类,Lazy<T>单例模式,IOC容器Singleton我们该如何选
单例模式·c#·.net core
gc_229921 小时前
C#测试调用OpenXml操作word文档的基本用法
c#·word·openxml
almighty271 天前
C#海康车牌识别实战指南带源码
c#·海康车牌识别·c#实现车牌识别·车牌识别源码·c#车牌识别
c#上位机1 天前
wpf之TextBlock
c#·wpf
almighty272 天前
C# WinForm分页控件实现与使用详解
c#·winform·分页控件·c#分页·winform分页