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

相关推荐
weixin_4487717239 分钟前
使用xml模板导出excel
xml·java·excel
yagerfgcs1 小时前
【配置Visual Assistant VC助手 for Visual Studio 2017】
ide·c#·visual studio
PfCoder3 小时前
WinForm真入门(13)——ListBox控件详解
windows·c#·visual studio·winform
佟格湾6 小时前
聊透多线程编程-线程池-6.C# APM(异步编程模型)
开发语言·后端·c#·多线程
我不是程序猿儿7 小时前
【C#】一种优雅的基于winform的串口通信管理
stm32·单片机·c#
东方芷兰10 小时前
JavaWeb 课堂笔记 —— 08 请求响应
xml·java·笔记·spring·tomcat·html·idea
向宇it12 小时前
【unity游戏开发入门到精通——动画篇】Animator反向动力学(IK)
开发语言·unity·c#·编辑器·游戏引擎
Liu_某13 小时前
c# 运用策略模式与模板方法模式实例
c#·策略模式
du fei15 小时前
C# 组件的使用方法
java·开发语言·c#
Eiceblue15 小时前
使用Python写入JSON、XML和YAML数据到Excel文件
xml·开发语言·vscode·python·json·excel·pip