.net xml 增删改查基础复习及干货分享
Posted yq_Hua
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net xml 增删改查基础复习及干货分享相关的知识,希望对你有一定的参考价值。
今天做做项目时,有一个需求需要用到一些固定的文本数据,觉得将这些需要存储的信息直接写在代码里很不友好,放在数据库中存储又觉得不够方便,自然就想到了使用xml来进行操作,我平常在项目中其实用到xml的机会并不多,今天既然碰到了,就再温故知新一下吧....
xml简述
xml被设计用来传输和存储数据。(html被设计用来显示数据)
- xml指可扩展标记语言(Extensible Markup Language)
- xml是一种标记语言,很类似html
- xml的设计宗旨是传输数据,而非显示数据
- xml标签没有被预定义。您需要自行定义标签
- xml被设计为具有自我描述性
- xml是w3c的推荐标准
C#操作XML的基本操作:增删改查
demo示例代码:
<?xml version="1.0" encoding="utf-8"> <info> <userinfo> <name>花哥</name> <age>26</age> <email>921442206@qq.com</email> <website>51chiheng.com</website> </userinfo> </info>
增加:
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("myfolder/userinfo.xml"));//加载xml文件
XmlNode root = xmld.SelectSingleNode("info");//查找info根节点
XmlElement xml0 = xmld.CreateElement("userinfo");
xml0.SetAttribute("realname", "花**");
XmlElement xml1 = xmld.CreateElement("age");//创建一个age节点
xml1.InnerText ="26";
xml0.AppendChild(xml1);//添加
XmlElement xml2 = xmld.CreateElement("email");//创建emial节点
xml2.InnerText ="921442206@qq.com";
xml0.AppendChild(xml2);//添加
XmlElement xml3 = xmld.CreateElement("website");//创建一个website节点
xml3.InnerText = "51chiheng.com";
xml0.AppendChild(xml3);//添加
root.AppendChild(xml0);
xmld.Save(Server.MapPath("myfolder/userinfo.xml"));
删除操作:
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("myfolder/userinfo.xml"));
XmlNodeList list = xmld.SelectSingleNode("info").ChildNodes;
foreach (XmlNode var in list)
{
XmlElement xmle = (XmlElement)var;
if (xmle.GetAttribute("readname") == "花**")
{
xmle.RemoveAll();
xmld.Save(Server.MapPath("myfolder/userinfo.xml"));//删除后记得一定要保存
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert(\'删除成功!!\');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert(\'删除失败!!\');", true);
}
}
修改操作:
XmlDocument xmld = new XmlDocument(); xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode xmln in list)
{
XmlElement xmle = (XmlElement)xmln;//格式转换
if ("花**".Equals(xmle.GetAttribute("readname")))
{
xmle.ChildNodes[0].InnerText = "111";
xmle.ChildNodes[1].InnerText = "222";
xmle.ChildNodes[2].InnerText = "333";
xmle.ChildNodes[3].InnerText = "444";
xmld.Save(Server.MapPath("myfolder/userinfo.xml"));
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert(\'修改成功!!!\');location.href=\'index.aspx\'",true);
}
}
查询操作:
//下面的代码开始是用页面编码的方式实现的
XmlDocument xmld = new XmlDocument();//构建xml文档
xmld.Load(Server.MapPath("App_Code/Message.xml"));//加载
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;//获取子节点
if (list!=null)
{
foreach (XmlNode node in list)
{
XmlElement xmle = (XmlElement)node;//转换
//todo....
}
}
最后分享一点干货,聪明的程序员会学会站在巨人的肩膀上去眺望远方,而不是一直闷头苦干的造轮胎,感谢苏飞大神!
/// <summary> /// 好东西分享: xml帮助类大全 /// 类说明:【感谢苏飞大侠分享的好东西】 /// </summary> using System; using System.Data; using System.IO; using System.Xml; namespace DotNet.Utilities { public class XMLProcess { #region 构造函数 public XMLProcess() { } public XMLProcess(string strPath) { this._XMLPath = strPath; } #endregion #region 公有属性 private string _XMLPath; public string XMLPath { get { return this._XMLPath; } } #endregion #region 私有方法 /// <summary> /// 导入XML文件 /// </summary> /// <param name="XMLPath">XML文件路径</param> private XmlDocument XMLLoad() { string XMLFile = XMLPath; XmlDocument xmldoc = new XmlDocument(); try { string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile; if (File.Exists(filename)) xmldoc.Load(filename); } catch (Exception e) { } return xmldoc; } /// <summary> /// 导入XML文件 /// </summary> /// <param name="XMLPath">XML文件路径</param> private static XmlDocument XMLLoad(string strPath) { XmlDocument xmldoc = new XmlDocument(); try { string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + strPath; if (File.Exists(filename)) xmldoc.Load(filename); } catch (Exception e) { } return xmldoc; } /// <summary> /// 返回完整路径 /// </summary> /// <param name="strPath">Xml的路径</param> private static string GetXmlFullPath(string strPath) { if (strPath.IndexOf(":") > 0) { return strPath; } else { return System.Web.HttpContext.Current.Server.MapPath(strPath); } } #endregion #region 读取数据 /// <summary> /// 读取指定节点的数据 /// </summary> /// <param name="node">节点</param> /// 使用示列: /// XMLProsess.Read("/Node", "") /// XMLProsess.Read("/Node/Element[@Attribute=\'Name\']") public string Read(string node) { string value = ""; try { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); value = xn.InnerText; } catch { } return value; } /// <summary> /// 读取指定路径和节点的串联值 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// 使用示列: /// XMLProsess.Read(path, "/Node", "") /// XMLProsess.Read(path, "/Node/Element[@Attribute=\'Name\']") public static string Read(string path, string node) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); value = xn.InnerText; } catch { } return value; } /// <summary> /// 读取指定路径和节点的属性值 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// 使用示列: /// XMLProsess.Read(path, "/Node", "") /// XMLProsess.Read(path, "/Node/Element[@Attribute=\'Name\']", "Attribute") public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { } return value; } /// <summary> /// 获取某一节点的所有孩子节点的值 /// </summary> /// <param name="node">要查询的节点</param> public string[] ReadAllChildallValue(string node) { int i = 0; string[] str = { }; XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点 if (nodelist.Count > 0) { str = new string[nodelist.Count]; foreach (XmlElement el in nodelist)//读元素值 { str[i] = el.Value; i++; } } return str; } /// <summary> /// 获取某一节点的所有孩子节点的值 /// </summary> /// <param name="node">要查询的节点</param> public XmlNodeList ReadAllChild(string node) { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点 return nodelist; } /// <summary> /// 读取XML返回经排序或筛选后的DataView /// </summary> /// <param name="strWhere">筛选条件,如:"name=\'kgdiwss\'"</param> /// <param name="strSort"> 排序条件,如:"Id desc"</param> public DataView GetDataViewByXml(string strWhere, string strSort) { try { string XMLFile = this.XMLPath; string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile; DataSet ds = new DataSet(); ds.ReadXml(filename); DataView dv = new DataView(ds.Tables[0]); //创建DataView来完成排序或筛选操作 if (strSort != null) { dv.Sort = strSort; //对DataView中的记录进行排序 } if (strWhere != null) { dv.RowFilter = strWhere; //对DataView中的记录进行筛选,找到我们想要的记录 } return dv; } catch (Exception) { return null; } } /// <summary> /// 读取XML返回DataSet /// </summary> /// <param name="strXmlPath">XML文件相对路径</param> public DataSet GetDataSetByXml(string strXmlPath) { try { DataSet ds = new DataSet(); ds.ReadXml(GetXmlFullPath(strXmlPath)); if (ds.Tables.Count > 0) { return ds; } return null; } catch (Exception) { return null; } } #endregion #region 插入数据 /// <summary> /// 插入数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Insert(path, "/Node", "Element", "", "Value") /// XMLProsess.Insert(path, "/Node", "Element", "Attribute", "Value") /// XMLProsess.Insert(path, "/Node", "", "Attribute", "Value") public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); XmlNode xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (!attribute.Equals("")) { XmlElement xe = (XmlElement)xn; xe.SetAttribute(attribute, value); } } else { XmlElement xe = doc.CreateElement(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 插入数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param name="strList">由XML属性名和值组成的二维数组</param> public static void Insert(string path, string node, string element, string[][] strList) { try { XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = doc.CreateElement(element); string strAttribute = ""; string strValue = ""; for (int i = 0; i < strList.Length; i++) { for (int j = 0; j < strList[i].Length; j++) { if (j == 0) strAttribute = strList[i][j]; else strValue = strList[i][j]; } if (strAttribute.Equals("")) xe.InnerText = strValue; else xe.SetAttribute(strAttribute, strValue); } xn.AppendChild(xe); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 插入一行数据 /// </summary> /// <param name="strXmlPath">XML文件相对路径</param> /// <param name="Columns">要插入行的列名数组,如:string[] Columns = {"name","IsMarried"};</param> /// <param name="ColumnValue">要插入行每列的值数组,如:string[] ColumnValue={"XML大全","false"};</param> /// <returns>成功返回true,否则返回false</returns> public static bool WriteXmlByDataSet(string strXmlPath, string[] Columns, string[] ColumnValue) { try { //根据传入的XML路径得到.XSD的路径,两个文件放在同一个目录下 string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd"; DataSet ds = new DataSet(); ds.ReadXmlSchema(GetXmlFullPath(strXsdPath)); //读XML架构,关系到列的数据类型 ds.ReadXml(GetXmlFullPath(strXmlPath)); DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); //在原来的表格基础上创建新行 for (int i = 0; i < Columns.Length; i++) //循环给一行中的各个列赋值 { newRow[Columns[i]] = ColumnValue[i]; } dt.Rows.Add(newRow); dt.AcceptChanges(); ds.AcceptChanges(); ds.WriteXml(GetXmlFullPath(strXmlPath)); return true; } catch (Exception) { return false; } } #endregion #region 修改数据 /// <summary> /// 修改指定节点的数据 /// </summary> /// <param name="node">节点</param> /// <param name="value">值</param> public void Update(string node, string value) { try { XmlDocument doc = XMLLoad(); XmlNode xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLPath); } catch { } } /// <summary> /// 修改指定节点的数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Insert(path, "/Node","Value") /// XMLProsess.Insert(path, "/Node","Value") public static void Update(string path, string node, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); xn.InnerText = value; doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 修改指定节点的属性值(静态) /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Insert(path, "/Node", "", "Value") /// XMLProsess.Insert(path, "/Node", "Attribute", "Value") public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } /// <summary> /// 更改符合条件的一条记录 /// </summary> /// <param name="strXmlPath">XML文件路径</param> /// <param name="Columns">列名数组</param> /// <param name="ColumnValue">列值数组</param> /// <param name="strWhereColumnName">条件列名</param> /// <param name="strWhereColumnValue">条件列值</param> public static bool UpdateXmlRow(string strXmlPath, string[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue) { try { string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd"; DataSet ds = new DataSet(); ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));//读XML架构,关系到列的数据类型 ds.ReadXml(GetXmlFullPath(strXmlPath)); //先判断行数 if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { //如果当前记录为符合Where条件的记录 if (ds.Tables[0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue)) { //循环给找到行的各列赋新值 for (int j = 0; j < Columns.Length; j++) { ds.Tables[0].Rows[i][Columns[j]] = ColumnValue[j]; } ds.AcceptChanges(); //更新DataSet ds.WriteXml(GetXmlFullPath(strXmlPath));//重新写入XML文件 return true; } } } return false; } catch (Exception) { return false; } } #endregion #region 删除数据 /// <summary> /// 删除节点值 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param> /// <param name="value">值</param> /// 使用示列: /// XMLProsess.Delete(path, "/Node", "") /// XMLProsess.Delete(path, "/Node", "Attribute") public static void Delete(string path, string node) { try { XmlDocument doc = XMLLoad(path); XmlNode xn = doc.SelectSingleNode(node); xn.ParentNode.RemoveChild(xn); doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path); } catch { } } <以上是关于.net xml 增删改查基础复习及干货分享的主要内容,如果未能解决你的问题,请参考以下文章