C# XML
XMLDocument
儲存XML整份文件
XMLNode
表示 XML 文件中的單一節點。
文件上指出XmlNode是 DOM 的.NET 實作中的基底類別它支援 XPath 選取項目,並提供編輯功能。 XmlDocument類別會擴充XmlNode和代表 XML 文件。您可以使用XmlDocument載入和儲存 XML 資料。它也包含節點的建立方法。
XMLElement
XMLElement繼承自System.Xml.XmlNode,同樣是XML文件的任一節點。
文件上指出 XmlElement 類別有許多存取屬性 (Attribute) 的方法 (GetAttribute、SetAttribute、RemoveAttribute、GetAttributeNode 等等)。您也可以使用傳回XmlAttributeCollection 的 Attributes 屬性 (Property),使您可以用名稱或索引從集合中存取屬性 (Attribute)。
using System.Xml;
string strXML = "來源XML";
//reading XML
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(strXML);
//choose node using XPath
XmlNode xnode = xdoc.SelectSingleNode("Documents/Company");
if (xnode == null)
throw new Exception("無此節點");
//get element to access attributes
XmlElement xelement = (XmlElement)xnode;
//get innerText
strResultSql = xelement.InnerText;