xml linq
这里讲解一下linq对xml的基本操作,包括:
- 新建xml、新建节点
- 查询节点
- 插入属性、插入节点
- 替换节点
在这里我封装了一些常用的方法:
public class XmlHelper { /// <summary> /// xml路径名 /// </summary> public string XmlPath { get; set; } /// <summary> /// 初始化XmlPath /// </summary> /// <param name="xmlPath"></param> public XmlHelper(string xmlPath) { XmlPath = string.IsNullOrEmpty(xmlPath) ? "./my.xml" : xmlPath; } /// <summary> /// 初始化XML文件,如果原来有内容会被重置 /// </summary> /// <param name="root">root名</param> public void InitXml(string root = null) { if (!File.Exists(XmlPath)) { File.Create(XmlPath).Close(); } if (root == null) { root = @"root"; } XDocument xdoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement(root, null) ); xdoc.Save(XmlPath); } /// <summary> /// 创建Element /// </summary> /// <param name="elementName">ElementName</param> /// <param name="elementValue">ElementValue</param> /// <param name="xAttributes">属性数组(可选)</param> /// <returns></returns> public XElement CreateElement(string elementName, string elementValue, params XAttribute[] xAttributes) { XElement xElement = new XElement(elementName, elementValue); if (xAttributes != null) { foreach (XAttribute xAttribute in xAttributes) { xElement.SetAttributeValue(xAttribute.Name, xAttribute.Value); } } return xElement; } /// <summary> /// 查询第一个指定itemName的节点 /// </summary> /// <param name="itemName"></param> /// <returns></returns> public XElement QueryFirstElementByName(string itemName) { XElement xele = XElement.Load(XmlPath); if (xele.Name == itemName) { return xele; } else { XElement xele1 = xele.Element("item"); return xele1; } } /// <summary> /// 查询查询第一个指定itemName、itemValue的节点 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <returns></returns> public XElement QueryElementByNameAndValue(string itemName, string itemValue) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).FirstOrDefault(); return item; } /// <summary> /// 查询节点,通过itemName 和属性 /// </summary> /// <param name="itemName"></param> /// <param name="strAttributeName"></param> /// <param name="strAttributeValue"></param> /// <returns></returns> public XElement QueryElementByNameAndAttri(string itemName, string strAttributeName, string strAttributeValue) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Attribute(strAttributeName).Value == strAttributeValue select ele).FirstOrDefault(); return item; } /// <summary> /// 在指定节点后添加节点 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <param name="xElement"></param> public void AddToElementAfter(string itemName, string itemValue, XElement xElement) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).SingleOrDefault(); if (item != null) { item.AddAfterSelf(xElement); } xele.Save(XmlPath); } /// <summary> /// 在指定节点前添加节点 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <param name="xElement"></param> public void AddToElementBefore(string itemName, string itemValue, XElement xElement) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).SingleOrDefault(); if (item != null) { item.AddBeforeSelf(xElement); } xele.Save(XmlPath); } /// <summary> /// 在指定节点名下添加节点 /// </summary> /// <param name="itemName"></param> /// <param name="xElement"></param> public void AddElement(string itemName, XElement xElement) { XElement xele = XElement.Load(XmlPath); if (itemName == xele.Name) { xele.Add(xElement); } else { XElement item = xele.Element(itemName); if (item != null) { item.Add(xElement); } } //var item = (from ele in xele.Elements(itemName) // where ele.Value.Equals(null) // select ele).SingleOrDefault(); xele.Save(XmlPath); } /// <summary> /// 在指定节点下添加节点 /// </summary> /// <param name="rootElement"></param> /// <param name="xElement"></param> public void AddElement(XElement rootElement, XElement xElement) { XElement xele = XElement.Load(XmlPath); if (xele.Name == rootElement.Name) { xele.Add(xElement); } else { var root = xele.Element(rootElement.Name.LocalName); if (root != null) { root.Add(xElement); } } xele.Save(XmlPath); } /// <summary> /// 为指定节点添加属性 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <param name="attributeName"></param> /// <param name="attributeValue"></param> public void AddAttributeToEle(string itemName, string itemValue, string attributeName, string attributeValue) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).SingleOrDefault(); if (item != null) { item.SetAttributeValue(attributeName, attributeValue); } xele.Save(XmlPath); } /// <summary> /// 在指定节点后添加注释 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <param name="xcomment"></param> public void AddCommentToAfter(string itemName, string itemValue, string xcomment) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).SingleOrDefault(); if (item != null) { XComment xcom = new XComment(xcomment); item.AddAfterSelf(xcom); xele.Save(XmlPath); } } /// <summary> /// 在指定节点前添加注释 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <param name="xcomment"></param> public void AddCommentToBefore(string itemName, string itemValue, string xcomment) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).SingleOrDefault(); if (item != null) { XComment xcom = new XComment(xcomment); item.AddBeforeSelf(xcom); xele.Save(XmlPath); } } /// <summary> /// 替换指定节点 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <param name="xElement"></param> public void ReplaceElement(string itemName, string itemValue, XElement xElement) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).FirstOrDefault(); if (item != null) { item.ReplaceWith(xElement); } xele.Save(XmlPath); } /// <summary> /// 移除指定属性 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> /// <param name="strAttribute"></param> public void RemoveAttribute(string itemName, string itemValue, string strAttribute) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).FirstOrDefault().Attribute(strAttribute); if (item != null) { item.Remove(); } xele.Save(XmlPath); } /// <summary> /// 移除指定节点 /// </summary> /// <param name="itemName"></param> /// <param name="itemValue"></param> public void RemoveElement(string itemName, string itemValue) { XElement xele = XElement.Load(XmlPath); var item = (from ele in xele.Elements(itemName) where ele.Value.Equals(itemValue) select ele).FirstOrDefault(); if (item != null) { item.Remove(); } xele.Save(XmlPath); } }
经过验证,速度还是挺快的,但是如果要短时间操作大量数据的话,就应该自己封装一下自己需要的功能来加快速度,或者使用其他方法。
身是菩提树,心如明镜台,时时勤拂拭,勿使惹尘埃。