非空时删除该节点属性值

时间:2022-05-16 01:44:30

在.net的项目开发中,经常会对XML文件进行操纵,由于XML文件可以实现跨平台传输,较多的应用在数据传输中,特总结以下几种常用的XML操纵要领:

1.创建XML文档:

/// <summary> /// 创建XML文档 /// </summary> /// <param>根节点名称</param> /// <param>根节点的一个属性值</param> /// <returns>XmlDocument东西</returns> public static XmlDocument CreateXmlDocument(string name, string type) { XmlDocument doc; try { doc = new XmlDocument(); doc.LoadXml("<" + name + "/>"); var rootEle = doc.DocumentElement; rootEle?.SetAttribute("type", type); } catch (Exception er) { throw new Exception(er.ToString()); } return doc; }

2.读取XML文档中的数据:

/// <summary> /// 读取数据 /// </summary> /// <param>路径</param> /// <param>节点</param> /// <param>属性名,非空时返回该属性值,否则返回串联值</param> /// <returns>string</returns> public static string Read(string path, string node, string attribute) { var value = ""; try { var doc = new XmlDocument(); doc.Load(path); var xn = doc.SelectSingleNode(node); if (xn != null && xn.Attributes != null) value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch (Exception er) { throw new Exception(er.ToString()); } return value; }

3.对XML文档插入数据:

/// <summary> /// 插入数据 /// </summary> /// <param>路径</param> /// <param>节点</param> /// <param>元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param>属性名,非空时插入该元素属性值,,否则插入元素值</param> /// <param>值</param> /// <returns></returns> public static void Insert(string path, string node, string element, string attribute, string value) { try { var doc = new XmlDocument(); doc.Load(path); var xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (!attribute.Equals("")) { var xe = (XmlElement)xn; xe?.SetAttribute(attribute, value); //xe?.SetAttribute(attribute, value); } } else { var xe = doc.CreateElement(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn?.AppendChild(xe); } doc.Save(path); } catch (Exception er) { throw new Exception(er.ToString()); } }

4.改削XML文档中的数据:

/// <summary> /// 改削数据 /// </summary> /// <param>路径</param> /// <param>节点</param> /// <param>属性名,非空时改削该节点属性值,否则改削节点值</param> /// <param>值</param> /// <returns></returns> public static void Update(string path, string node, string attribute, string value) { try { var doc = new XmlDocument(); doc.Load(path); var xn = doc.SelectSingleNode(node); var xe = (XmlElement)xn; if (attribute.Equals("")) { if (xe != null) xe.InnerText = value; } else { xe?.SetAttribute(attribute, value); } doc.Save(path); } catch (Exception er) { throw new Exception(er.ToString()); } }

5.删除XML文档中数据:

/// <summary> /// 删除数据 /// </summary> /// <param>路径</param> /// <param>节点</param> /// <param>属性名,非空时删除该节点属性值,否则删除节点值</param> /// <returns></returns> public static void Delete(string path, string node, string attribute) { try { var doc = new XmlDocument(); doc.Load(path); var xn = doc.SelectSingleNode(node); var xe = (XmlElement)xn; if (attribute.Equals("")) { xn?.ParentNode?.RemoveChild(xn); } else { xe?.RemoveAttribute(attribute); } doc.Save(path); } catch (Exception er) { throw new Exception(er.ToString()); } }

6.读取XML文档中指定节点数据: