Ai
4 Star 11 Fork 8

鸣飞/BuildH框架代码开发辅助工具

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
XmlHelper.cs 4.36 KB
一键复制 编辑 原始数据 按行查看 历史
鸣飞 提交于 2019-02-12 16:31 +08:00 . xxx
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
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/mf_/CodeGenerator.git
git@gitee.com:mf_/CodeGenerator.git
mf_
CodeGenerator
BuildH框架代码开发辅助工具
master

搜索帮助