封裝常用 XML 操作的靜態工具類,涵蓋 `XmlTextWriter` 建立檔案、`XmlDocument` 新增/讀取節點與屬性、完整例外處理。
using System;
using System.Xml;
using System.Web;
namespace BLL
{
public class XmlHelper
{
#region 建立 XML 檔案
///
/// 建立新的 XML 檔案,寫入根元素與宣告
///
/// 實體路徑 (Server.MapPath 解析後)
/// 根元素名稱
public static void CreateXmlFile(string xmlPath, string rootElement)
{
using (XmlTextWriter writer = new XmlTextWriter(xmlPath, System.Text.Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement(rootElement);
writer.WriteEndElement();
}
}
#endregion
#region 新增節點
///
/// 在指定 XPath 節點下新增子節點 (傳入 XmlNode)
///
public static void AddNode(string xmlPath, string parentXPath, XmlNode childNode)
{
XmlDocument doc = LoadDocument(xmlPath);
XmlNode parent = doc.SelectSingleNode(parentXPath);
if (parent != null)
{
XmlNode imported = doc.ImportNode(childNode, true);
parent.AppendChild(imported);
doc.Save(xmlPath);
}
}
///
/// 在指定 XPath 節點下新增元素與屬性
///
public static void AddElement(string xmlPath, string parentXPath, string elementName, string attributeName, string attributeValue)
{
XmlDocument doc = LoadDocument(xmlPath);
XmlNode parent = doc.SelectSingleNode(parentXPath);
if (parent != null)
{
XmlElement elem = doc.CreateElement(elementName);
elem.SetAttribute(attributeName, attributeValue);
parent.AppendChild(elem);
doc.Save(xmlPath);
}
}
#endregion
#region 讀取操作
private static XmlDocument LoadDocument(string xmlPath)
{
string physicalPath = HttpContext.Current != null
? HttpContext.Current.Server.MapPath(xmlPath)
: xmlPath;
XmlDocument doc = new XmlDocument();
doc.Load(physicalPath);
return doc;
}
/// 取得完整 XmlDocument
public static XmlDocument GetDocument(string xmlPath)
{
try { return LoadDocument(xmlPath); }
catch (XmlException) { throw; }
catch (System.IO.IOException) { throw; }
}
/// 依 XPath 取得單一 XmlNode
public static XmlNode GetNode(string xmlPath, string xPath)
{
try { return LoadDocument(xmlPath).SelectSingleNode(xPath); }
catch (XPathException) { throw; }
catch (XmlException) { throw; }
catch (System.IO.IOException) { throw; }
}
/// 依 XPath 取得子元素 XmlElement
public static XmlElement GetElement(string xmlPath, string parentXPath, string childElementName)
{
try
{
XmlNode parent = LoadDocument(xmlPath).SelectSingleNode(parentXPath);
return parent?[childElementName] as XmlElement;
}
catch (XPathException) { throw; }
catch (XmlException) { throw; }
catch (System.IO.IOException) { throw; }
}
/// 讀取節點屬性值
public static string GetAttribute(string xmlPath, string xPath, string attributeName)
{
try
{
XmlNode node = LoadDocument(xmlPath).SelectSingleNode(xPath);
return node?.Attributes[attributeName]?.Value;
}
catch (XPathException) { throw; }
catch (XmlException) { throw; }
catch (System.IO.IOException) { throw; }
}
#endregion
}
}
// 1. 建立檔案
XmlHelper.CreateXmlFile(Server.MapPath("~/App_Data/Config.xml"), "Settings");
// 2. 新增節點
XmlDocument doc = new XmlDocument();
XmlElement dbNode = doc.CreateElement("Database");
dbNode.SetAttribute("Provider", "SqlServer");
XmlHelper.AddNode(Server.MapPath("~/App_Data/Config.xml"), "/Settings", dbNode);
// 3. 新增帶屬性元素
XmlHelper.AddElement(Server.MapPath("~/App_Data/Config.xml"), "/Settings", "Connection", "Name", "Default");
// 4. 讀取
string provider = XmlHelper.GetAttribute(Server.MapPath("~/App_Data/Config.xml"), "/Settings/Database", "Provider");
XmlElement connElem = XmlHelper.GetElement(Server.MapPath("~/App_Data/Config.xml"), "/Settings", "Connection");
大型 XML 或高效能需求請改用 `XmlReader`/`XmlWriter` 串流模式或 `LINQ to XML` (`XDocument`)。