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

相关推荐
QQ12958455044 小时前
C# 如何能够创建一个MVC的WEB项目
c#·mvc
星河队长7 小时前
VS创建C++动态库和C#访问过程
java·c++·c#
William_cl7 小时前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
yong99908 小时前
C#驱动斑马打印机实现包装自动打印
java·数据库·c#
Jose_lz8 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#
mingupup9 小时前
WPF/C#:使用Microsoft Agent Framework框架创建一个带有审批功能的终端Agent
c#·wpf
YuanlongWang11 小时前
C# 设计模式——单例模式
单例模式·设计模式·c#
YuanlongWang11 小时前
C#基础——GC(垃圾回收)的工作流程与优化策略
java·jvm·c#
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ12 小时前
mapper.xml sql动态表查询配置
xml·java·sql
YuanlongWang12 小时前
C# 基础——多态的实现方式
java·c#