XML操作类
public class XmlControl:Object
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();
public XmlControl(string XmlFile, Boolean bOverWrite, string sRoot)
{
try
{
//如果覆盖模式,则强行创建一个xml文档
if (bOverWrite)
{
objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));//设置xml的版本,格式信息
objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));//创建根元素
objXmlDoc.Save(XmlFile);//保存
}
else //否则,检查文件是否存在,不存在则创建
{
if (!(File.Exists(XmlFile)))
{
objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));
objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));
objXmlDoc.Save(XmlFile);
}
}
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
public DataView GetData(string XmlPathNode)
{
//查找数据。返回一个DataView
DataSet ds = new DataSet();
try
{
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds.Tables[0].DefaultView;
}
catch
{
return null;
}
}
public void UpdateNode(string xmlPathNode, string content)
{
//更新节点內容,如果多个相同的节点,则更新第一个节点
objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
}
public void UpdateNode(string xmlPathNode, string[] arrAttribName, string[] arrAttribContent, string content)
{
//更新节点,带属性和内容
//修改内容
objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
//修改属性
for (int i = 0; i <= arrAttribName.GetUpperBound(0); i++)
{
objXmlDoc.SelectSingleNode(xmlPathNode).Attributes[i].Value = arrAttribContent[i];
}
}
public void DeleteNode(string Node)
{
//刪除一个节点。
try
{
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
catch {
//throw;
return;
}
}
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);//插入子节点
}
public void InsertNode(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);
}
public void InsertNode(string mainNode, string Element, string Content)
{
//插入一个节点,不带属性。
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(elementName);
for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
{
objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
}
objElement.InnerText = elementContent;
objNode.AppendChild(objElement);
}
public void Save()
{
//保存文档。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
}
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();
public XmlControl(string XmlFile, Boolean bOverWrite, string sRoot)
{
try
{
//如果覆盖模式,则强行创建一个xml文档
if (bOverWrite)
{
objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));//设置xml的版本,格式信息
objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));//创建根元素
objXmlDoc.Save(XmlFile);//保存
}
else //否则,检查文件是否存在,不存在则创建
{
if (!(File.Exists(XmlFile)))
{
objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));
objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));
objXmlDoc.Save(XmlFile);
}
}
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
public DataView GetData(string XmlPathNode)
{
//查找数据。返回一个DataView
DataSet ds = new DataSet();
try
{
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds.Tables[0].DefaultView;
}
catch
{
return null;
}
}
public void UpdateNode(string xmlPathNode, string content)
{
//更新节点內容,如果多个相同的节点,则更新第一个节点
objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
}
public void UpdateNode(string xmlPathNode, string[] arrAttribName, string[] arrAttribContent, string content)
{
//更新节点,带属性和内容
//修改内容
objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
//修改属性
for (int i = 0; i <= arrAttribName.GetUpperBound(0); i++)
{
objXmlDoc.SelectSingleNode(xmlPathNode).Attributes[i].Value = arrAttribContent[i];
}
}
public void DeleteNode(string Node)
{
//刪除一个节点。
try
{
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
catch {
//throw;
return;
}
}
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);//插入子节点
}
public void InsertNode(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);
}
public void InsertNode(string mainNode, string Element, string Content)
{
//插入一个节点,不带属性。
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(elementName);
for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
{
objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
}
objElement.InnerText = elementContent;
objNode.AppendChild(objElement);
}
public void Save()
{
//保存文档。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述