usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;namespaceXmlReaderAndWriter
{public classXmlWriter
{static XmlDocument doc = newXmlDocument();///
///创建根节点///
/// 根节点的名称
/// 返回根节点
public static XmlElement CreateRootElement(stringxn)
{
XmlElement xe=doc.CreateElement(xn);
doc.AppendChild(xe);returnxe;
}///
///添加一个Attribute到一个XmlElement元素///
/// 被添加的XmlElement元素
/// Attribute的特证名
/// Attribute的值
public static void AppendAttributeToElement(XmlElement xe,string attrname,stringattrtext)
{
XmlAttribute xa=CreateAttribute(attrname,attrtext);
xe.Attributes.Append(xa);
}///
///添加一个Attribute到一个XmlElement元素///
/// 被添加的XmlElement元素
/// Attribute对象
public voidAppendAttributeToElement(XmlElement xe, XmlAttribute attr)
{
XmlAttribute xa=attr;
xe.Attributes.Append(xa);
}///
///创建一个Attribute对象///
/// Attribute的特证名
/// Attribute的值
/// 返回创建的Attribute对象
public static XmlAttribute CreateAttribute(string attrname,stringattrtext)
{
XmlAttribute xa=doc.CreateAttribute(attrname);
xa.InnerText=attrtext;returnxa;
}///
///创建一个具有指定名称和值的节点///
/// XmlNode的名称
/// XmlNode的值
/// 返回XmlNode对象
public static XmlNode CreateElement(string name, stringtext)
{
XmlNode xn= (XmlNode)doc.CreateElement(name); //创建具有指定名称的元素并转换成节点
xn.InnerText = text; //获取节点中的Text
returnxn;
}///
///添加新节点到旧节点之后///
/// 旧节点
/// 新节点
public static voidXmlInsertAfter(XmlNode oldxn,XmlNode newxn)
{
XmlNode parent= oldxn.ParentNode; //获取被添加节点的父节点
parent.InsertAfter(newxn, oldxn); //添加新节点到旧节点之后
}///
///添加新节点到旧节点之前///
/// 旧节点
/// 新节点
public static voidXmlInsertBefore(XmlNode oldxn, XmlNode newxn)
{
XmlNode parent= oldxn.ParentNode; //获取被添加节点的父节点
parent.InsertBefore(newxn, oldxn);//添加新节点到旧节点之前
}///
///添加子节点到指定节点中///
/// 指定的父节点
/// 要添加进的子节点
public static voidAppendChild(XmlNode parentnode, XmlNode childnode)
{
parentnode.AppendChild(childnode);
}///
///创建Xml文件声明节点,必须要调用此方法///
/// 版本号,必须为1.0
/// 字符编码
/// 独立特性的值
public static void CreateXmlDeclaration(string version,string encoding, stringstandalone)
{
XmlDeclaration xd;
xd=doc.CreateXmlDeclaration(version, encoding, standalone); //创建声明节点
if (doc.ChildNodes == null) //如果存在根节点
{
doc.AppendChild(xd);//添加根节点
}else{
XmlElement root= doc.DocumentElement; //获取文档的根节点
doc.RemoveAll(); //移除所有节点
doc.AppendChild(xd); //添加声明节点
doc.AppendChild(root); ///添加根节点
}
}///
///移除指定的节点///
///
/// 返回移除结果
public static boolRemoveChildNode(XmlNode childnode)
{try{
XmlNode parentnode= childnode.ParentNode; //获取父节点
parentnode.RemoveChild(childnode); //移除父节点下的指定子节点
return true;
}catch{return false;
}
}///
///移除所有的节点///
/// 节点名称
///
public static boolRemoveChildAllNode(XmlNode xmlnode)
{
xmlnode.RemoveAll();//移除所有节点
if (xmlnode.ChildNodes.Count == 0)
{return true;
}else{return false;
}
}///
///选择具有指定名称的一个节点///
/// 父节点对象
/// 要查找节点的Name
/// 返回查找结果
public static XmlNode SelectSingleNode(XmlNode xn, stringxname)
{returnxn.SelectSingleNode(xname);
}///
///选择具有指定名称的多个节点///
/// 父节点对象
/// 要查找节点的Name
/// 返回查找结果
public static XmlNodeList SelectNodes(XmlNode xn, stringxname)
{returnxn.SelectNodes(xname);
}///
///移除节点中指定的Attribute///
/// XmlNode对象
/// Attribute的名称
/// 返回移除结果
public static bool RemoveXmlAttribute(XmlNode xn,stringxan)
{int ac=xn.Attributes.Count;
XmlNode xmlnode=xn.Attributes.RemoveNamedItem(xan);if (xmlnode.Attributes.Count == ac - 1)
{return true;
}else{return false;
}
}///
///移除节点中所有的Attribute///
/// XmlNode对象
/// 返回移除结果
public static boolRemoveXmlAttribute(XmlNode xn)
{
xn.Attributes.RemoveAll();if (xn.Attributes.Count == 0)
{return true;
}else{return false;
}
}///
///替换节点的值///
/// XmlNode对象
/// 新的XmlNode的值
public static void ReplaceText(XmlNode xn, stringtext)
{
xn.InnerText=text;
}///
///替换指定节点///
/// 新节点
/// 正在被替换的节点
/// 返回新节点
public staticXmlNode ReplaceChild(XmlNode nxn ,XmlNode oxn)
{returndoc.ReplaceChild(nxn, oxn);
}///
///保存Xml文件///
public static void SavaXml(stringurl)
{
doc.Save(url);
}
}
}