分享下C#操作xml文件实例,包括了对XML文件节点的查找、遍历、删除、添加等。
是C#程序设计中非常重要的技巧。
针对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>");
} (www.jbxue.com 脚本学堂)
}
}
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(""))
{(www.jbxue.com 脚本学堂)
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#操作XML文件的几个常见的示例。具体如下: 1.返回节点下标 public static XmlDocument getDoc(String path)//加载xml文档 { XmlDocument doc = new XmlDocument(); doc.Load(path); return ...
实例107 使用哈希表对XML文件进行查询 127 实例108 计算两个矩形矩阵的乘积 129 第5章 面向对象编程技术 131 5.1 属性和方法 132 实例109 使用属性存储用户编号和姓名 132 实例110 通过定义方法求一个数的平方 133 ...
C# xml操作 using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xml.Load(path); xml.Load(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));
用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。配置文件的根节点是 configuration。我们经常访问的是...
实例080 将XML文件节点绑定到TreeView控件中 106 2.8 DataGridView控件应用 108 实例081 DataGridView控件的分页功能 108 实例082 从DataGridView控件拖放数据至TreeView控件 113 实例083 在DataGridView控件中...
实例080 将XML文件节点绑定到TreeView控件中 106 2.8 DataGridView控件应用 108 实例081 DataGridView控件的分页功能 108 实例082 从DataGridView控件拖放数据至TreeView控件 113 实例083 在DataGridView控件中...
实例080 将XML文件节点绑定到TreeView控件中 106 2.8 DataGridView控件应用 108 实例081 DataGridView控件的分页功能 108 实例082 从DataGridView控件拖放数据至TreeView控件 113 实例083 在DataGridView控件中...
实例080 将XML文件节点绑定到TreeView控件中 106 2.8 DataGridView控件应用 108 实例081 DataGridView控件的分页功能 108 实例082 从DataGridView控件拖放数据至TreeView控件 113 实例083 在DataGridView控件中...