栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#操作XML文件实例汇总

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#操作XML文件实例汇总

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

1.返回节点下标

public static Xmldocument getDoc(String path)//加载xml文档
{
  Xmldocument doc = new Xmldocument();
  doc.Load(path);
  return doc;
} 
/// 
/// 返回找到的节点下标
/// 
/// xml文件路径
/// 书名
/// 
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.遍历数据

/// 
/// 遍历数据
/// 
/// 
/// 
protected void btnRead_Click(object sender, EventArgs e)
{

  Xmldocument doc = getDoc("books.xml");//加载xml文档,books文件存放于
  foreach (XmlElement root in doc.documentElement.ChildNodes)//遍历根的子节点
  {
 Response.Write("");
 Response.Write("");//节点名root.Name book
 foreach (XmlElement item in root.ChildNodes)
 {
   Response.Write("");//输出节点名和文本节点的值
   Response.Write("");
 }
  }
}

3.查找

/// 
/// 查找
/// 
/// 文件路径
/// 节点
/// 查找关键字
/// XmlNode
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.删除节点

/// 
/// 删除元素、属性
/// 
/// 文件
/// 指定节点的父节点
/// 为空时删除节点,否则删除属性
/// 
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.添加

/// 
/// 添加元素值
/// 
/// 
/// 
/// 
/// 
/// 插入的下标,如果为负,默认从最后一个节点插入
/// 
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#程序设计能有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/126209.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号