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

相关推荐
为自己_带盐1 小时前
在 Blazor Server 中集成 docx-preview.js 实现高保真 Word 预览
javascript·c#·word
chilavert3182 小时前
技术演进中的开发沉思-304计算机原理:XML
xml·计算机原理
hixiong1232 小时前
C# OpenvinoSharp部署DDDDOCR验证码识别模型
opencv·c#·ocr·openvino
唐青枫3 小时前
C#.NET ConcurrentBag<T> 设计原理与使用场景
c#·.net
玩泥巴的12 小时前
飞书 .NET SDK 事件处理的幂等性与去重机制
c#·.net·二次开发·飞书
在路上看风景12 小时前
3.2 FileStream
c#
zwm26988881512 小时前
6号楼 部分声光24v电压达不到,显示11v
c#
椒颜皮皮虾16 小时前
TensorRtSharp:在 C# 世界中释放 GPU 推理的极致性能
c#·tensorrt
行止9516 小时前
WinForms 彻底隐藏 滚动条的终极解决方案
c#