欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C#編寫一個(gè)操作XML的簡單類庫XMLHelper

 更新時(shí)間:2023年06月26日 08:34:02   作者:BoiledYakult  
這篇文章主要為大家詳細(xì)介紹了如何基于C#編寫一個(gè)操作XML的簡單類庫——XMLHelper,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下

下午寫了一個(gè)操作XML文件的類庫,后來不用了,水篇文章存?zhèn)€檔

整體功能

XMLHelper.cs主要提供以下功能:

  • 加載XML文件:從文件路徑或字符串中加載XML文檔,并返回XmlDocument對象。
  • 保存XML文件:將XmlDocument對象保存為XML文件。
  • 讀取XML文件到DataTable:將XML文件中的數(shù)據(jù)讀取到DataTable對象中。
  • 生成XML文件:生成一個(gè)新的XML文件,并指定根節(jié)點(diǎn)名稱。
  • DataTable更新XML文件:將DataTable對象中的數(shù)據(jù)更新到XML文件中。
  • 獲取節(jié)點(diǎn)值:根據(jù)XPath表達(dá)式獲取指定節(jié)點(diǎn)的值。
  • 設(shè)置節(jié)點(diǎn)值:根據(jù)XPath表達(dá)式設(shè)置指定節(jié)點(diǎn)的值。
  • 獲取屬性值:根據(jù)XPath表達(dá)式和屬性名稱獲取指定節(jié)點(diǎn)的屬性值。
  • 設(shè)置屬性值:根據(jù)XPath表達(dá)式和屬性名稱設(shè)置指定節(jié)點(diǎn)的屬性值。
  • 更新文件中的節(jié)點(diǎn)值:根據(jù)文件路徑、XPath表達(dá)式和值更新XML文件中的節(jié)點(diǎn)值。
  • 更新文件中的屬性值:根據(jù)文件路徑、XPath表達(dá)式、屬性名稱和值更新XML文件中的屬性值。

沒用 LINQ To XML 語法糖

XMLHelper.cs

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;
            }
        }
        // 從一個(gè)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);
            }
        }
        // 生成一個(gè)新的XML文件,并指定根節(jié)點(diǎn)名稱
        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);
            }
        }
        /// <summary>
        /// 讀取XML文件到DataTable:將XML文件中的數(shù)據(jù)讀取到DataTable對象中
        /// </summary>
        /// <param name="xmlDoc">XmlDocument對象</param>
        /// <param name="xpath">節(jié)點(diǎn)集合的 XPath 表達(dá)式 - 例如"/Roots/Child"</param>
        /// <returns>DataTable對象</returns>
        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;
            }
        }
        /// <summary>
        /// 將DataTable對象中的數(shù)據(jù)更新到XML文件中
        /// </summary>
        /// <param name="xmlDoc">XmlDocument對象</param>
        /// <param name="dataTable">DataTable對象</param>
        /// <param name="elementName">子節(jié)點(diǎn)值</param>
        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);
            }
        }
        // 根據(jù)XPath表達(dá)式獲取指定節(jié)點(diǎn)的值
        public string GetNodeValue(XmlDocument xmlDoc, string xpath)
        {
            XmlNode node = xmlDoc.SelectSingleNode(xpath);
            return node?.InnerText;
        }
        // 根據(jù)XPath表達(dá)式設(shè)置指定節(jié)點(diǎn)的值
        public void SetNodeValue(XmlDocument xmlDoc, string xpath, string value)
        {
            XmlNode node = xmlDoc.SelectSingleNode(xpath);
            if (node != null)
                node.InnerText = value;
        }
        // 根據(jù)XPath表達(dá)式和屬性名稱獲取指定節(jié)點(diǎn)的屬性值
        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;
        }
        // 根據(jù)XPath表達(dá)式和屬性名稱設(shè)置指定節(jié)點(diǎn)的屬性值
        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;
            }
        }
        // 根據(jù)文件路徑、XPath表達(dá)式和值更新XML文件中的節(jié)點(diǎn)值
        public void UpdateNodeValueInFile(string filePath, string xpath, string value)
        {
            XmlDocument doc = LoadXmlDocumentFromFile(filePath);
            if (doc != null)
            {
                SetNodeValue(doc, xpath, value);
                SaveXmlDocument(doc, filePath);
            }
        }
        // 根據(jù)文件路徑、XPath表達(dá)式、屬性名稱和值更新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);
            }
        }
    }
}

異常處理就需要大家自由發(fā)揮了

加載和保存XML文件

XMLHelper類庫提供了兩個(gè)方法,用于從文件路徑或字符串中加載XML文檔并返回XmlDocument對象,分別是:

public XmlDocument LoadXmlDocumentFromFile(string filePath)
public XmlDocument LoadXmlDocumentFromString(string xmlString)

可以使用這些方法將XML文件加載到XmlDocument對象中,方便后續(xù)的處理和操作,一個(gè)操作文件,一個(gè)操作XML結(jié)構(gòu)的字符串,然后保存:

public void SaveXmlDocument(XmlDocument xmlDoc, string filePath)

這幾個(gè)都是調(diào)用直接的方法,沒什么可說的。

讀取和更新XML文件

XMLHelper類庫使得從XML文件中讀取數(shù)據(jù)變得非常簡單。其中,ReadXmlToDataTable方法允許將XML文件中的數(shù)據(jù)讀取到DataTable對象中:

public DataTable ReadXmlToDataTable(XmlDocument xmlDoc, string xpath)

只需要提供XmlDocument對象和節(jié)點(diǎn)集合的XPath表達(dá)式(例如"/Roots/Child"),即可將XML文件中的數(shù)據(jù)讀取到DataTable對象中。

另外,可以使用UpdateXmlFromDataTable方法將DataTable對象中的數(shù)據(jù)更新到XML文件中:

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

這個(gè)方法會清空XML文件并根據(jù)DataTable對象中的數(shù)據(jù)創(chuàng)建新的XML節(jié)點(diǎn),并將其添加到XmlDocument對象中。

示例用法

// 創(chuàng)建XMLHelper對象
XMLHelper xmlHelper = new XMLHelper();
// 加載XML文件
XmlDocument xmlDoc = xmlHelper.LoadXmlDocumentFromFile("data.xml");
// 讀取XML數(shù)據(jù)到DataTable
DataTable dataTable = xmlHelper.ReadXmlToDataTable(xmlDoc, "/Root/Element");
// 修改節(jié)點(diǎn)的值
dataTable.Rows[0]["Value"] = "New Value";
// 更新XML文件
xmlHelper.UpdateXmlFromDataTable(xmlDoc, dataTable, "Element");
// 保存XML文件
xmlHelper.SaveXmlDocument(xmlDoc, "data.xml");

讀取和更新XML節(jié)點(diǎn)的值

XMLHelper還提供了一些方法用于讀取和更新XML節(jié)點(diǎn)的值。以下是其中幾個(gè)方法的示例:

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

這些方法允許你根據(jù)XPath表達(dá)式獲取節(jié)點(diǎn)的文本值或?qū)傩灾担⑶铱梢愿鹿?jié)點(diǎn)的文本值或?qū)傩灾怠?/p>

示例用法

// 創(chuàng)建XMLHelper對象
XMLHelper xmlHelper = new XMLHelper();
// 加載XML文件
XmlDocument xmlDoc = xmlHelper.LoadXmlDocumentFromFile("data.xml");
// 讀取節(jié)點(diǎn)的值
string nodeValue = xmlHelper.GetNodeValue(xmlDoc, "/Root/Element/Value");
Console.WriteLine("Node Value: " + nodeValue);
// 更新節(jié)點(diǎn)的值
string newValue = "New Value";
xmlHelper.SetNodeValue(xmlDoc, "/Root/Element/Value", newValue);
Console.WriteLine("Node Value updated.");
// 保存XML文件
xmlHelper.SaveXmlDocument(xmlDoc, "data.xml");

生成XML文件

除了加載、讀取和更新XML文件,XMLHelper類庫還提供了一個(gè)方法用于生成XML文件。你可以使用GenerateXmlFile方法創(chuàng)建一個(gè)空的XML文件,指定根元素的名稱和文件路徑:

public void GenerateXmlFile(string filePath, string rootElementName)

這個(gè)有點(diǎn)瑕疵,后面用到再改哈。

設(shè)計(jì)思路

ChatGPT did this :

LoadXmlDocumentFromFile(string filePath): XmlDocument

  • 功能:從指定文件路徑加載XML文檔。
  • 輸入:文件路徑。
  • 輸出:加載成功返回XmlDocument對象,加載失敗返回null。
  • 設(shè)計(jì)思路:使用XmlDocument的Load方法從文件路徑加載XML文檔,如果加載失敗,則返回null。

LoadXmlDocumentFromString(string xmlString): XmlDocument

  • 功能:從指定字符串加載XML文檔。
  • 輸入:XML字符串。
  • 輸出:加載成功返回XmlDocument對象,加載失敗返回null。
  • 設(shè)計(jì)思路:使用XmlDocument的LoadXml方法從字符串加載XML文檔,如果加載失敗,則返回null。

GenerateXmlFile(string filePath, string rootElementName): void

  • 功能:生成一個(gè)新的XML文件,并指定根節(jié)點(diǎn)名稱。
  • 輸入:文件路徑,根節(jié)點(diǎn)名稱。
  • 輸出:無。
  • 設(shè)計(jì)思路:創(chuàng)建一個(gè)新的XmlDocument對象,并使用指定的根節(jié)點(diǎn)名稱創(chuàng)建根節(jié)點(diǎn)。然后使用XmlDocument的Save方法將XmlDocument對象保存為指定文件路徑的XML文件。

SaveXmlDocument(XmlDocument xmlDoc, string filePath): void

  • 功能:將XmlDocument對象保存為XML文件。
  • 輸入:XmlDocument對象,文件路徑。
  • 輸出:無。
  • 設(shè)計(jì)思路:使用XmlDocument的Save方法將XmlDocument對象保存為指定文件路徑的XML文件。

ReadXmlToDataTable(XmlDocument xmlDoc): DataTable

  • 功能:將XML文件中的數(shù)據(jù)讀取到DataTable對象中。
  • 輸入:XmlDocument對象。
  • 輸出:讀取成功返回DataTable對象,讀取失敗返回null。
  • 設(shè)計(jì)思路:使用XmlDocument的SelectNodes方法選取指定XPath表達(dá)式的節(jié)點(diǎn)集合,遍歷節(jié)點(diǎn)集合,并根據(jù)節(jié)點(diǎn)的子節(jié)點(diǎn)創(chuàng)建DataTable的列。然后遍歷每個(gè)節(jié)點(diǎn),將子節(jié)點(diǎn)的名稱和文本內(nèi)容作為DataTable的行數(shù)據(jù)。最后返回DataTable對象。

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

  • 功能:將DataTable對象中的數(shù)據(jù)更新到XML文件中。
  • 輸入:XmlDocument對象,DataTable對象,要更新的節(jié)點(diǎn)元素名稱。
  • 輸出:無。
  • 設(shè)計(jì)思路:首先清空XmlDocument的根節(jié)點(diǎn)下的所有子節(jié)點(diǎn)。然后遍歷DataTable的每一行,創(chuàng)建一個(gè)新的XmlElement,并根據(jù)DataTable的列名和行數(shù)據(jù)設(shè)置XmlElement的子節(jié)點(diǎn)。最后將新的XmlElement添加到XmlDocument的根節(jié)點(diǎn)下。

GetNodeValue(XmlDocument xmlDoc, string xpath): string

  • 功能:根據(jù)XPath表達(dá)式獲取指定節(jié)點(diǎn)的值。
  • 輸入:XmlDocument對象,XPath表達(dá)式。
  • 輸出:節(jié)點(diǎn)的值,如果節(jié)點(diǎn)不存在則返回null。
  • 設(shè)計(jì)思路:使用XmlDocument的SelectSingleNode方法根據(jù)XPath表達(dá)式選取指定節(jié)點(diǎn),然后返回節(jié)點(diǎn)的InnerText。

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

  • 功能:根據(jù)XPath表達(dá)式設(shè)置指定節(jié)點(diǎn)的值。
  • 輸入:XmlDocument對象,XPath表達(dá)式,要設(shè)置的值。
  • 輸出:無。
  • 設(shè)計(jì)思路:使用XmlDocument的SelectSingleNode方法根據(jù)XPath表達(dá)式選取指定節(jié)點(diǎn),然后將節(jié)點(diǎn)的InnerText設(shè)置為指定的值。

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

  • 功能:根據(jù)XPath表達(dá)式和屬性名稱獲取指定節(jié)點(diǎn)的屬性值。
  • 輸入:XmlDocument對象,XPath表達(dá)式,屬性名稱。
  • 輸出:屬性的值,如果屬性不存在則返回null。
  • 設(shè)計(jì)思路:使用XmlDocument的SelectSingleNode方法根據(jù)XPath表達(dá)式選取指定節(jié)點(diǎn),然后根據(jù)屬性名稱獲取屬性的值。

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

  • 功能:根據(jù)XPath表達(dá)式和屬性名稱設(shè)置指定節(jié)點(diǎn)的屬性值。
  • 輸入:XmlDocument對象,XPath表達(dá)式,屬性名稱,要設(shè)置的值。
  • 輸出:無。
  • 設(shè)計(jì)思路:使用XmlDocument的SelectSingleNode方法根據(jù)XPath表達(dá)式選取指定節(jié)點(diǎn),然后根據(jù)屬性名稱設(shè)置屬性的值。

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

  • 功能:根據(jù)文件路徑、XPath表達(dá)式和值更新XML文件中的節(jié)點(diǎn)值。
  • 輸入:文件路徑,XPath表達(dá)式,要設(shè)置的值。
  • 輸出:無。
  • 設(shè)計(jì)思路:首先從文件路徑加載XML文檔,然后調(diào)用SetNodeValue函數(shù)設(shè)置指定節(jié)點(diǎn)的值,最后保存XML文檔到文件。

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

  • 功能:根據(jù)文件路徑、XPath表達(dá)式、屬性名稱和值更新XML文件中的屬性值。
  • 輸入:文件路徑,XPath表達(dá)式,屬性名稱,要設(shè)置的值。
  • 輸出:無。
  • 設(shè)計(jì)思路:首先從文件路徑加載XML文檔,然后調(diào)用SetAttributeValue函數(shù)設(shè)置指定節(jié)點(diǎn)的屬性值,最后保存XML文檔到文件。

到此這篇關(guān)于基于C#編寫一個(gè)操作XML的簡單類庫XMLHelper的文章就介紹到這了,更多相關(guān)C#操作XML內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論