這是一個基於 COM 模型思路、對 XML 檔案進行完整操作的 C#.NET 類別封裝。以下貼出實現 XML 檔案操作的 C#.NET 程式碼,方便大家參考與優化。
/*
* Description: 完全的操作 XML 檔案
* Auther: mingziweb_天很藍
* Email: chongchong2008@msn.com
* Dates: 2004-09-10
* Copyright: ChongChong2008 YiChang HuBei China
*/
using System;
using System.XML;
using System.XML.Xsl;
using System.XML.XPath;
using System.Web;
namespace Library.ClassLibrary.XML
{
/// <summary>
/// XML 的摘要說明。
/// </summary>
public class XML
{
public enum enumXmlPathType
{
AbsolutePath,
VirtualPath
}
private string XMLFilePath;
private enumXmlPathType XMLFilePathType;
private XmlDocument xmlDoc = new XmlDocument();
public string XMLFilePath
{
set { XMLFilePath = value; }
}
public enumXmlPathType XMLFilePathTyp
{
set { XMLFilePathType = value; }
}
public XML(string tempXMLFilePath)
{
this.xmlFilePathType = enumXmlPathType.VirtualPath;
this.xmlFilePath = tempXMLFilePath;
GetXMLDocument();
}
public XML(string tempXmlFilePath, enumXmlPathType tempXMLFilePathType)
{
this.xmlFilePathType = tempXMLFilePathType;
this.xmlFilePath = tempXmlFilePath;
GetXMLDocument();
}
// 依路徑類型取得 XML 文件物件
private XmlDocument GetXMLDocument()
{
XmlDocument doc = null;
if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)
{
doc = GetXmlDocumentFromFile(XMLFilePath);
}
else if (this.xmlFilePathType == enumXmlPathType.VirtualPath)
{
doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(XMLFilePath));
}
return doc;
}
private XmlDocument GetXmlDocumentFromFile(string tempXMLFilePath)
{
string xmlFileFullPath = tempXMLFilePath;
xmlDoc.Load(xmlFileFullPath);
return xmlDoc;
}
// 取得 XML 文件的根元素
public XmlNode GetXMLRoot()
{
return xmlDoc.DocumentElement;
}
// 在根節點下新增父節點
public void AddParentNode(string parentNode)
{
XmlNode root = GetXMLRoot();
XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);
root.AppendChild(parentXmlNode);
}
// 向已存在的父節點中插入子節點
public void AddChildNode(string parentNodePath, string childNodePath)
{
XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);
XmlNode childXmlNode = xmlDoc.CreateElement(childNodePath);
parentXmlNode.AppendChild(childXmlNode);
}
// 向節點新增屬性
public void AddAttribute(string NodePath, string NodeAttribute)
{
XmlAttribute nodeAttribute = xmlDoc.CreateAttribute(NodeAttribute);
XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);
nodePath.Attributes.Append(nodeAttribute);
}
// 刪除節點
public void DeleteXmlNode(string tempXMLNode)
{
XmlNode xmlNodePath = xmlDoc.SelectSingleNode(tempXMLNode);
xmlNodePath.ParentNode.RemoveChild(xmlNodePath);
}
// 讀取指定節點的指定屬性值
public string GetXMLNodeValue(string strNode, string strAttribute)
{
string strReturn = "";
try
{
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
XmlAttributeCollection xmlAttr = xmlNode.Attributes;
for (int i = 0; i < xmlAttr.Count; i++)
{
if (xmlAttr.Item(i).Name == strAttribute)
strReturn = xmlAttr.Item(i).Value;
}
}
catch (XmlException XMLe)
{
throw XMLe;
}
return strReturn;
}
// 讀取指定節點的值
public string GetXMLNodeValue(string strNode)
{
string strReturn = String.Empty;
try
{
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
strReturn = xmlNode.InnerText;
}
catch (XmlException XMLe)
{
System.Console.WriteLine(XMLe.Message);
}
return strReturn;
}
// 設定節點值
public void SetXmlNodeValue(string xmlNodePath, string XMLNodeValue)
{
try
{
XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath);
xmlNode.InnerText = XMLNodeValue;
}
catch (XmlException XMLe)
{
throw XMLe;
}
}
}
}
這個類別封裝了 XML 檔案的載入、根節點取得、新增父子節點、新增與刪除屬性、讀寫節點值與屬性值等常見操作。由於是早期作品,讀者可在其基礎上進一步優化,例如補上完整的屬性刪除邏輯與更完善的例外處理。