C#操作XML文件实例汇总

作者:shichen2014 时间:2023-03-26 03:55:08 

针对XML文件的操作是C#程序设计中非常常见的功能。本文即以实例展示了C#操作XML文件的几个常见的示例。具体如下:

1.返回节点下标


public static XmlDocument getDoc(String path)//加载xml文档
{
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 return doc;
}
/// <summary>
/// 返回找到的节点下标
/// </summary>
/// <param name="path">xml文件路径</param>
/// <param name="bname">书名</param>
/// <returns></returns>
public static int getPosition(String path,string node, String bname)
{
 int i;
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;
 for (i = 0; i < nodeList.Count; i++)
 {
if (nodeList[i].ChildNodes[0].InnerText == bname)
{
  return i;
}
 }
 return -1;
}

2.遍历数据


/// <summary>
/// 遍历数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnRead_Click(object sender, EventArgs e)
{

XmlDocument doc = getDoc("books.xml");//加载xml文档,books文件存放于
 foreach (XmlElement root in doc.DocumentElement.ChildNodes)//遍历根的子节点
 {
Response.Write("<script>document.write('<br/>');</script>");
Response.Write("<script>document.write('" + root.Name + "');</script>");//节点名root.Name book
foreach (XmlElement item in root.ChildNodes)
{
  Response.Write("<script>document.write('" + item.Name + "'+':'+'" + item.InnerText + "');</script>");//输出节点名和文本节点的值
  Response.Write("<script>document.write('<br/>');</script>");
}
 }
}

3.查找


/// <summary>
/// 查找
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="node">节点</param>
/// <param name="bname">查找关键字</param>
/// <returns>XmlNode</returns>
public static XmlNode Find(string path,string node,string bname)
{
 XmlDocument doc = new XmlDocument();
 doc.Load(path);//加载xml文档
 XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;
 int i = getPosition(path, node, bname);//
 if (i >= 0)
return nodeList[i];
 else
return null;
}

4.删除节点


/// <summary>
/// 删除元素、属性
/// </summary>
/// <param name="path">文件</param>
/// <param name="node">指定节点的父节点</param>
/// <param name="attribute">为空时删除节点,否则删除属性</param>
/// <param name="bname"></param>
public static void Delete(string path,string node,string attribute,string bname)
{
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 XmlNode root = doc.SelectSingleNode(node);
 XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;
 int i = getPosition(path, node, bname);//返回指定的节点下标
 if (i >= 0)
 {
if (attribute.Equals(""))
{
  root.RemoveChild(nodeList[i]);
}
else
{
  XmlElement xn = (XmlElement)nodeList[i];
  xn.RemoveAttribute(attribute);
}
 }
 doc.Save(path);
}

5.添加


/// <summary>
/// 添加元素值
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <param name="element"></param>
/// <param name="value"></param>
/// <param name="i">插入的下标,如果为负,默认从最后一个节点插入</param>
/// <returns></returns>
public static bool Add(string path,string node,string element,string value,int i)
{
 XmlDocument doc = new XmlDocument();
 doc.Load(path);
 XmlNodeList nodeList = doc.SelectNodes(node);
 XmlNode newNode = doc.SelectSingleNode(node).LastChild;
 if (i < 0 || i > nodeList.Count-1)//如果小于0或者大于节点长度,默认从最后一个节点添加
 {
XmlElement newElement = doc.CreateElement(element);//创建元素
newElement.InnerText = value;//赋值
newNode.AppendChild(newElement);
 }
 else
 {
XmlElement newElement = doc.CreateElement(element);
newElement.InnerText = value;
nodeList[i - 1].AppendChild(newElement);
 }
 doc.Save(path);
 return true;
}

希望本文所述方法对大家的C#程序设计能有所帮助。

标签:C#,操作,XML
0
投稿

猜你喜欢

  • Java编程经典小游戏设计-打砖块小游戏源码

    2021-07-08 01:17:28
  • 详解Android应用沙盒机制

    2023-01-30 10:43:37
  • java开发WMS仓库商品预警需求示例解析

    2023-10-03 11:07:30
  • C#实现读取DataSet数据并显示在ListView控件中的方法

    2023-08-24 01:23:16
  • Android基于Intent实现Activity之间数据传递的方法

    2022-08-01 16:34:04
  • Winform应用程序如何使用自定义的鼠标图片

    2021-07-09 16:01:24
  • java获取当前时间的四种方法代码实例

    2023-11-28 19:22:53
  • C# 根据字符串生成二维码的实例代码

    2023-09-16 09:06:50
  • Winform项目中TextBox控件DataBindings属性

    2023-03-29 15:25:15
  • android开发环境遇到adt无法启动的问题分析及解决方法

    2023-12-11 13:13:23
  • Android编程基于自定义View实现绚丽的圆形进度条功能示例

    2023-07-24 17:13:26
  • C#封装的Sqlite访问类实例

    2022-04-28 15:38:13
  • 解决IDEA和CMD中java命令提示错误: 找不到或无法加载主类的问题

    2023-09-19 02:31:16
  • C#实现图形界面的时钟

    2022-10-03 15:10:50
  • IDEA2022.2的简介、下载与安装、配置教程

    2023-01-13 21:21:03
  • Android 模仿iPhone列表数据View刷新动画详解

    2021-10-05 07:57:47
  • c# 编写的简单飞行棋游戏

    2022-12-05 05:10:59
  • java二维数组遍历的2种代码

    2022-05-03 08:52:50
  • java实现微信H5支付方法详解

    2021-10-15 06:10:30
  • Java应用EasyExcel工具类

    2023-07-07 18:45:32
  • asp之家 软件编程 m.aspxhome.com