欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#操作XML文件实例汇总

程序员文章站 2023-12-22 10:48:40
针对xml文件的操作是c#程序设计中非常常见的功能。本文即以实例展示了c#操作xml文件的几个常见的示例。具体如下: 1.返回节点下标 public stati...

针对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#程序设计能有所帮助。

上一篇:

下一篇: