·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> xml使用系统整理
1、 认识xml
可扩展标记语言,一种用于标记电子文档使其具有结果性的标记语言,它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。
2、 和超文本标记语言区别
2.1 html不一定需要成对出现,xml则一定需要成对出现。
2.2 html 不区分大小写,但是xml区分。
3、对xml文档增删改查
1 //声明一个XmlDocument空对象 2 PRotected XmlDocument XmlDoc = new XmlDocument(); 3 /// <summary> 4 /// 构造函数,导入xml文件 5 /// </summary> 6 /// <param name="path"></param> 7 public XmlHelper(string path) 8 { 9 try10 {11 XmlDoc.Load(path);12 }13 catch (Exception ex)14 {15 throw ex;16 }17 }18 /// <summary>19 /// 保存文件20 /// </summary>21 /// <param name="path"></param>22 public void SaveXml(string path)23 {24 try25 {26 XmlDoc.Save(path);27 }28 catch (System.Exception ex)29 {30 throw ex;31 }32 }
1 /// <summary> 2 /// 获取节点的子节点的内容 3 /// </summary> 4 /// <param name="path"></param> 5 /// <param name="rootNode"></param> 6 /// <param name="attributeName"></param> 7 /// <returns></returns> 8 public string GetNodeChildAttribute(string path, string rootNode, string attributeName) 9 {10 XmlNode xn = XmlDoc.SelectSingleNode(rootNode);11 StringBuilder sb = new StringBuilder();12 XmlNodeList xnl = xn.ChildNodes;13 14 foreach (XmlNode xnf in xnl)15 {16 XmlElement xe = (XmlElement)xnf;17 XmlNodeList xnf1 = xe.ChildNodes;18 foreach (XmlNode xn2 in xnf1)19 {20 if (xn2.Name == attributeName)21 {22 sb.Append(xn2.InnerText);//显示子节点点文本23 }24 }25 }26 return sb.ToString();27 }
1 /// <summary> 2 /// 获取节点的属性值 3 /// </summary> 4 /// <param name="path">xml路径</param> 5 /// <param name="rootNode">根节点名称</param> 6 /// <param name="attributeName">属性名称</param> 7 /// <returns></returns> 8 public string GetNodeAttribute(string path, string rootNode, string attributeName) 9 {10 try11 {12 XmlNode xn = XmlDoc.SelectSingleNode(rootNode);13 XmlNodeList xnl = xn.ChildNodes;14 StringBuilder sb = new StringBuilder();15 foreach (XmlNode xnf in xnl)16 {17 XmlElement xe = (XmlElement)xnf;18 sb.Append(xe.GetAttribute(attributeName));19 }20 return sb.ToString();21 }22 catch (Exception)23 {24 25 throw;26 }27 }
1 /// <summary> 2 /// 删除节点/节点属性 3 /// </summary> 4 /// <param name="path">xml文件地址</param> 5 /// <param name="rootNode">根节点名称</param> 6 /// <param name="delNode">要删除的节点</param> 7 /// <param name="attributeName">节点属性</param> 8 /// <param name="attributeValue">属性值</param> 9 public void DeleteNode(string path, string rootNode, string attributeName, string attributeValue)10 {11 try12 {13 XmlNodeList xnl = XmlDoc.SelectSingleNode(rootNode).ChildNodes;14 foreach (XmlNode xn in xnl)15 {16 XmlElement xe = (XmlElement)xn;17 if (xe.GetAttribute(attributeName) == attributeValue)18 {19 //xe.RemoveAttribute(attributeName);//删除属性20 xe.RemoveAll();//删除该节点的全部内容21 }22 }23 SaveXml(path);24 }25 catch (Exception)26 {27 28 throw;29 }30 }
1 /// <summary> 2 /// 修改节点的子节点内容 3 /// </summary> 4 /// <param name="path">xml文件路径</param> 5 /// <param name="rootNode">根节点名称</param> 6 /// <param name="attributeName">节点的子节点名称</param> 7 /// <param name="attributeOldValue">节点的子节点原始内容</param> 8 /// <param name="attributeNewValue">节点的子节点新内容</param> 9 public void UpdateChildNodeAttribute(string path, string rootNode, string attributeName, string attributeOldValue, string attributeNewValue)10 {11 try12 {13 XmlNodeList nodeList = XmlDoc.SelectSingleNode(rootNode).ChildNodes;//获取根节点的所有子节点14 foreach (XmlNode xn in nodeList)//遍历所有子节点15 {16 XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型17 if (string.IsNullOrEmpty(attributeName) || string.IsNullOrEmpty(attributeOldValue))18 {19 return;20 }21 else22 {23 XmlNodeList nls = xe.ChildNodes;24 if (nls != null && nls.Count > 0)25 {26 foreach (XmlNode xn1 in nls)//遍历27 {28 XmlElement xe2 = (XmlElement)xn1;//转换类型29 if (xe2.InnerText == attributeOldValue)//如果找到30 {31 xe2.InnerText = attributeNewValue;//则修改32 break;//找到退出来就可以了33 }34 }35 }36 }37 }38 SaveXml(path);39 }40 catch (Exception)41 {42 43 throw;44 }45 }
1 /// <summary> 2 /// 修改节点属性值操作 3 /// </summary> 4 /// <param name="path">xml文件路径</param> 5 /// <param name="rootNode">根节点名称,如:bookstore</param> 6 /// <param name="attributeName">节点属性名</param> 7 /// <param name="attributeOldValue">节点属性原来值</param> 8 /// <param name="attributeNewValue">节点属性修改后的值</param> 9 public void UpdateNodeAttribute(string path, string rootNode, string attributeName, string attributeOldValue, string attributeNewValue)10 {11 try12 {13 XmlNodeList nodeList = XmlDoc.SelectSingleNode(rootNode).ChildNodes;//获取根节点的所有子节点14 foreach (XmlNode xn in nodeList)//遍历所有子节点15 {16 XmlElement xe = (XmlElement)xn;//将子节点类型专程xmlelement类型17 if (string.IsNullOrEmpty(attributeName) || string.IsNullOrEmpty(attributeOldValue))18 {19 return;20 }21 else22 {23 if (xe.GetAttribute(attributeName) == attributeOldValue)24 {25 xe.SetAttribute(attributeName, attributeNewValue);26 }27 }28 }29 SaveXml(path);30 }31 catch (Exception)32 {33 34 throw;35 }36 }
1 /// <summary> 2 /// 插入节点操作 3 /// </summary> 4 /// <param name="path">xml文件路径</param> 5 /// <param name="rootN