XML:Extensible Markup Language(可擴充标記語言)的縮寫,是用來定義其它語言的一種元語言,其前身是SGML(Standard Generalized Markup Language,标準通用标記語言)。它沒有标簽集(tag set),也沒有文法規則(grammatical rule),但是它有句法規則(syntax rule)。
任何XML文檔對任何類型的應用以及正确的解析都必須是良構的(well-formed),即每一個打開的标簽都必須有比對的結束标簽,不得含有次序颠倒的标簽,并且在語句構成上應符合技術規範的要求。XML文檔可以是有效的(valid),但并非一定要求有效。所謂有效文檔是指其符合其文檔類型定義(DTD)的文檔。如果一個文檔符合一個模式(schema)的規定,那麼這個文檔是"模式有效的(schema valid)"。
XML檔案在存儲、交換和傳輸資料資訊上有着很友善處理,下面用的是C#語言對XML檔案的基本操作,如:建立xml檔案,增、删、改、查xml的節點資訊。
//需要導入的命名空間
using System.Xml;
using System.IO;
/// <summary>
/// 對XML檔案進行增、删、改、查
/// </summary>
public static void XMLOperate()
{
try
{
//xml檔案存儲路徑
string myXMLFilePath = "E:\\MyComputers.xml";
//生成xml檔案
GenerateXMLFile(myXMLFilePath);
//周遊xml檔案的資訊
GetXMLInformation(myXMLFilePath);
//修改xml檔案的資訊
ModifyXmlInformation(myXMLFilePath);
//向xml檔案添加節點資訊
AddXmlInformation(myXMLFilePath);
//删除指定節點資訊
DeleteXmlInformation(myXMLFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
/// <summary>
/// 生成xml檔案
/// </summary>
/// <param name="xmlFilePath"></param>
private static void GenerateXMLFile(string xmlFilePath)
{
try
{
//初始化一個xml執行個體
XmlDocument myXmlDoc = new XmlDocument();
//建立xml的根節點
XmlElement rootElement = myXmlDoc.CreateElement("Computers");
//将根節點加入到xml檔案中(AppendChild)
myXmlDoc.AppendChild(rootElement);
//初始化第一層的第一個子節點
XmlElement firstLevelElement1 = myXmlDoc.CreateElement("Computer");
//填充第一層的第一個子節點的屬性值(SetAttribute)
firstLevelElement1.SetAttribute("ID", "11111111");
firstLevelElement1.SetAttribute("Description", "Made in China");
//将第一層的第一個子節點加入到根節點下
rootElement.AppendChild(firstLevelElement1);
//初始化第二層的第一個子節點
XmlElement secondLevelElement11 = myXmlDoc.CreateElement("name");
//填充第二層的第一個子節點的值(InnerText)
secondLevelElement11.InnerText = "Lenovo";
firstLevelElement1.AppendChild(secondLevelElement11);
XmlElement secondLevelElement12 = myXmlDoc.CreateElement("price");
secondLevelElement12.InnerText = "5000";
firstLevelElement1.AppendChild(secondLevelElement12);
XmlElement firstLevelElement2 = myXmlDoc.CreateElement("Computer");
firstLevelElement2.SetAttribute("ID", "2222222");
firstLevelElement2.SetAttribute("Description", "Made in USA");
rootElement.AppendChild(firstLevelElement2);
XmlElement secondLevelElement21 = myXmlDoc.CreateElement("name");
secondLevelElement21.InnerText = "IBM";
firstLevelElement2.AppendChild(secondLevelElement21);
XmlElement secondLevelElement22 = myXmlDoc.CreateElement("price");
secondLevelElement22.InnerText = "10000";
firstLevelElement2.AppendChild(secondLevelElement22);
//将xml檔案儲存到指定的路徑下
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
/// <summary>
/// 周遊xml檔案的資訊
/// </summary>
/// <param name="xmlFilePath"></param>
private static void GetXMLInformation(string xmlFilePath)
{
try
{
//初始化一個xml執行個體
XmlDocument myXmlDoc = new XmlDocument();
//加載xml檔案(參數為xml檔案的路徑)
myXmlDoc.Load(xmlFilePath);
//獲得第一個姓名比對的節點(SelectSingleNode):此xml檔案的根節點
XmlNode rootNode = myXmlDoc.SelectSingleNode("Computers");
//分别獲得該節點的InnerXml和OuterXml資訊
string innerXmlInfo = rootNode.InnerXml.ToString();
string outerXmlInfo = rootNode.OuterXml.ToString();
//獲得該節點的子節點(即:該節點的第一層子節點)
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//獲得該節點的屬性集合
XmlAttributeCollection attributeCol = node.Attributes;
foreach (XmlAttribute attri in attributeCol)
{
//擷取屬性名稱與屬性值
string name = attri.Name;
string value = attri.Value;
Console.WriteLine("{0} = {1}", name, value);
}
//判斷此節點是否還有子節點
if (node.HasChildNodes)
{
//擷取該節點的第一個子節點
XmlNode secondLevelNode1 = node.FirstChild;
//擷取該節點的名字
string name = secondLevelNode1.Name;
//擷取該節點的值(即:InnerText)
string innerText = secondLevelNode1.InnerText;
Console.WriteLine("{0} = {1}", name, innerText);
//擷取該節點的第二個子節點(用數組下标擷取)
XmlNode secondLevelNode2 = node.ChildNodes[1];
name = secondLevelNode2.Name;
innerText = secondLevelNode2.InnerText;
Console.WriteLine("{0} = {1}", name, innerText);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
/// <summary>
/// 修改xml檔案的資訊
/// </summary>
/// <param name="xmlFilePath"></param>
private static void ModifyXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
XmlNode rootNode = myXmlDoc.FirstChild;
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//修改此節點的屬性值
if (node.Attributes["Description"].Value.Equals("Made in USA"))
{
node.Attributes["Description"].Value = "Made in HongKong";
}
}
//要想使對xml檔案所做的修改生效,必須執行以下Save方法
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
/// <summary>
/// 向xml檔案添加節點資訊
/// </summary>
/// <param name="xmlFilePath"></param>
private static void AddXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
//添加一個帶有屬性的節點資訊
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
{
XmlElement newElement = myXmlDoc.CreateElement("color");
newElement.InnerText = "black";
newElement.SetAttribute("IsMixed", "Yes");
node.AppendChild(newElement);
}
//儲存更改
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
/// <summary>
/// 删除指定節點資訊
/// </summary>
/// <param name="xmlFilePath"></param>
private static void DeleteXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
{
//記錄該節點下的最後一個子節點(簡稱:最後子節點)
XmlNode lastNode = node.LastChild;
//删除最後子節點下的左右子節點
lastNode.RemoveAll();
//删除最後子節點
node.RemoveChild(lastNode);
}
//儲存對xml檔案所做的修改
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
結果如圖:
【注1:想要将對xml檔案所做的任何修改生效的話,必須調用Save方法,否則我們所做的修改不會儲存】
【注2:我們在建立節點的時候用的是XmlElement,但是讀取節點資訊的時候卻用的是XmlNode,這裡強調一點:XmlElement是XmlNode的繼承,可以調用更多的方法實作相應所需的功能】
下面簡單集中的總結一下對xml進行操作的基本方法,如下所示:
//所需要添加的命名空間
using System.Xml;
//初始化一個xml執行個體
XmlDocument xml=new XmlDocument();
//導入指定xml檔案
xml.Load(“xml檔案路徑path”);
//指定一個節點
XmlNode root=xml.SelectSingleNode("節點名稱");
//擷取節點下所有直接子節點
XmlNodeList childlist=root.ChildNodes;
//判斷該節點下是否有子節點
root.HasChildNodes;
//擷取同名同級節點集合
XmlNodeList nodelist=xml.SelectNodes("節點名稱");
//生成一個新節點
XmlElement node=xml.CreateElement("節點名稱");
//将節點加到指定節點下,作為其子節點
root.AppendChild(node);
//将節點加到指定節點下某個子節點前
root.InsertBefore(node,root.ChildeNodes[i]);
//為指定節點的建立屬性并指派
node.SetAttribute("id","11111");
//為指定節點添加子節點
root.AppendChild(node);
//擷取指定節點的指定屬性值
string id=node.Attributes["id"].Value;
//擷取指定節點中的文本
string content=node.InnerText;
//儲存XML檔案
xml.Save(“xml檔案存儲的路徑path”);