代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace CodeGenerator.Common
{
public class XmlHelper
{
#region Field
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();
#endregion
#region Constructors
public XmlHelper(string XmlFile)
{
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
#endregion
#region Method
/// <summary>
/// 读取节点内容
/// </summary>
/// <param name="XmlPathNode"></param>
/// <param name="Attrib"></param>
/// <returns></returns>
public string Read(string XmlPathNode, string Attrib)
{
string value = "";
try
{
XmlNode xn = objXmlDoc.SelectSingleNode(XmlPathNode);
value = (Attrib.Equals("") ? xn.InnerText : xn.Attributes[Attrib].Value);
}
catch (Exception ex)
{
throw ex;
}
return value;
}
/// <summary>
/// 替换某节点的内容
/// </summary>
/// <param name="XmlPathNode"></param>
/// <param name="Content"></param>
public void Replace(string XmlPathNode, string Content)
{
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}
/// <summary>
/// 删除节点
/// </summary>
/// <param name="Node"></param>
public void Delete(string Node)
{
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
/// <summary>
/// 插入一节点和此节点的一子节点
/// </summary>
/// <param name="MainNode"></param>
/// <param name="ChildNode"></param>
/// <param name="Element"></param>
/// <param name="Content"></param>
public void InsertNode(string MainNode, string ChildNode, string Element, string Content)
{
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
}
/// <summary>
/// 插入一个节点带一个属性
/// </summary>
/// <param name="MainNode"></param>
/// <param name="Element"></param>
/// <param name="Attrib"></param>
/// <param name="AttribContent"></param>
/// <param name="Content"></param>
public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
/// <summary>
/// 插入
/// </summary>
/// <param name="MainNode"></param>
/// <param name="Element"></param>
/// <param name="Content"></param>
public void InsertElement(string MainNode, string Element, string Content)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
/// <summary>
/// 保存XML
/// </summary>
public void Save()
{
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
#endregion
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。