基于C#编写一个操作XML的简单类库XMLHelper

  using System;

  using System.Data;

  using System.Xml;

  namespace XMLHelper

  {

  class XMLHelper

  {

  private XmlDocument xmlDoc;

  public XMLHelper()

  {

  xmlDoc = new XmlDocument();

  }

  // 从文件路径或字符串中加载XML文档,并返回XmlDocument对象

  public XmlDocument LoadXmlDocumentFromFile(string filePath)

  {

  try

  {

  xmlDoc.Load(filePath);

  return xmlDoc;

  }

  catch (Exception ex)

  {

  Console.WriteLine("Error loading XML document from file: " + ex.Message);

  return null;

  }

  }

  // 从一个XML字符串加载XML文档,并返回XmlDocument对象

  public XmlDocument LoadXmlDocumentFromString(string xmlString)

  {

  try

  {

  xmlDoc.LoadXml(xmlString);

  return xmlDoc;

  }

  catch (Exception ex)

  {

  Console.WriteLine("Error loading XML document from string: " + ex.Message);

  return null;

  }

  }

  // 保存XML文件:将XmlDocument对象保存为XML文件

  public void SaveXmlDocument(XmlDocument xmlDoc, string filePath)

  {

  try

  {

  xmlDoc.Save(filePath);

  }

  catch (Exception ex)

  {

  Console.WriteLine("Error saving XML document: " + ex.Message);

  }

  }

  // 生成一个新的XML文件,并指定根节点名称

  public void GenerateXmlFile(string filePath, string rootElementName)

  {

  try

  {

  xmlDoc = new XmlDocument();

  XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);

  XmlNode rootNode = xmlDoc.CreateElement(rootElementName);

  xmlDoc.AppendChild(xmlDeclaration);

  xmlDoc.AppendChild(rootNode);

  xmlDoc.Save(filePath);

  }

  catch (Exception ex)

  {

  Console.WriteLine("Error generating XML file: " + ex.Message);

  }

  }

  ///

  /// 读取XML文件到DataTable:将XML文件中的数据读取到DataTable对象中

  ///

  /// XmlDocument对象

  /// 节点集合的 XPath 表达式 - 例如"/Roots/Child"

  /// DataTable对象

  public DataTable ReadXmlToDataTable(XmlDocument xmlDoc,string xpath)

  {

  try

  {

  DataTable dataTable = new DataTable();

  XmlNodeList nodes = xmlDoc.SelectNodes(xpath);

  foreach (XmlNode node in nodes)

  {

  if (dataTable.Columns.Count == 0)

  {

  foreach (XmlNode childNode in node.ChildNodes)

  {

  dataTable.Columns.Add(childNode.Name, typeof(string));

  }

  }

  DataRow row = dataTable.NewRow();

  foreach (XmlNode childNode in node.ChildNodes)

  {

  row[childNode.Name] = childNode.InnerText;

  }

  dataTable.Rows.Add(row);

  }

  return dataTable;

  }

  catch (Exception ex)

  {

  Console.WriteLine("Error reading XML document to DataTable: " + ex.Message);

  return null;

  }

  }

  ///

  /// 将DataTable对象中的数据更新到XML文件中

  ///

  /// XmlDocument对象

  /// DataTable对象

  /// 子节点值

  public void UpdateXmlFromDataTable(XmlDocument xmlDoc, DataTable dataTable,string elementName)

  {

  try

  {

  xmlDoc.DocumentElement.RemoveAll();

  foreach (DataRow row in dataTable.Rows)

  {

  XmlElement measurementPointElement = xmlDoc.CreateElement(elementName);

  foreach (DataColumn column in dataTable.Columns)

  {

  XmlElement element = xmlDoc.CreateElement(column.ColumnName);

  element.InnerText = row[column.ColumnName].ToString();

  measurementPointElement.AppendChild(element);

  }

  xmlDoc.DocumentElement.AppendChild(measurementPointElement);

  }

  }

  catch (Exception ex)

  {

  Console.WriteLine("Error updating XML from DataTable: " + ex.Message);

  }

  }

  // 根据XPath表达式获取指定节点的值

  public string GetNodeValue(XmlDocument xmlDoc, string xpath)

  {

  XmlNode node = xmlDoc.SelectSingleNode(xpath);

  return node?.InnerText;

  }

  // 根据XPath表达式设置指定节点的值

  public void SetNodeValue(XmlDocument xmlDoc, string xpath, string value)

  {

  XmlNode node = xmlDoc.SelectSingleNode(xpath);

  if (node != null)

  node.InnerText = value;

  }

  // 根据XPath表达式和属性名称获取指定节点的属性值

  public string GetAttributeValue(XmlDocument xmlDoc, string xpath, string attributeName)

  {

  XmlNode node = xmlDoc.SelectSingleNode(xpath);

  if (node != null && node.Attributes != null)

  {

  XmlAttribute attribute = node.Attributes[attributeName];

  return attribute?.Value;

  }

  return null;

  }

  // 根据XPath表达式和属性名称设置指定节点的属性值

  public void SetAttributeValue(XmlDocument xmlDoc, string xpath, string attributeName, string value)

  {

  XmlNode node = xmlDoc.SelectSingleNode(xpath);

  if (node != null && node.Attributes != null)

  {

  XmlAttribute attribute = node.Attributes[attributeName];

  if (attribute != null)

  attribute.Value = value;

  }

  }

  // 根据文件路径、XPath表达式和值更新XML文件中的节点值

  public void UpdateNodeValueInFile(string filePath, string xpath, string value)

  {

  XmlDocument doc = LoadXmlDocumentFromFile(filePath);

  if (doc != null)

  {

  SetNodeValue(doc, xpath, value);

  SaveXmlDocument(doc, filePath);

  }

  }

  // 根据文件路径、XPath表达式、属性名称和值更新XML文件中的属性值

  public void UpdateAttributeValueInFile(string filePath, string xpath, string attributeName, string value)

  {

  XmlDocument doc = LoadXmlDocumentFromFile(filePath);

  if (doc != null)

  {

  SetAttributeValue(doc, xpath, attributeName, value);

  SaveXmlDocument(doc, filePath);

  }

  }

  }

  }