一 .Net框架中與XML有關(guān)的命名空間
System.Xml
包含了一些和XML文檔的讀寫(xiě)操作相關(guān)的類(lèi),它們分別是:XmlReader、XmlTextReader、XmlValidatingReader、XmlNodeReader、XmlWriter、XmlTextWriter 以及XmlNode(它的子類(lèi)包括:XmlDocument、XmlDataDocument、XmlDocumentFragment)等類(lèi)。
System.Xml.Schema
包含了和XML模式相關(guān)的類(lèi),這些類(lèi)包括XmlSchema、XmlSchemaAll、XmlSchemaXPath以及XmlSchemaType等類(lèi)。
System.Xml.Serialization
包含了和XML文檔的序列化和反序列化操作相關(guān)的類(lèi)。
序列化:將XML格式的數據轉化為流格式的數據,并能在網(wǎng)絡(luò )中傳輸;
反序列化:完成相反的操作,即將流格式的數據還原成XML格式的數據。
System.Xml.Xpath
包含了XPathDocument、XPathExression、XPathNavigator以及XPathNodeIterator等類(lèi),這些類(lèi)能完成XML文檔的導航功能。
(在XPathDocument類(lèi)的協(xié)助下,XPathNavigator類(lèi)能完成快速的XML文檔導航功能,該類(lèi)為程序員提供了許多Move方法以完成導航功能。)
System.Xml.Xsl
完成XSLT的轉換功能。
二 寫(xiě)XML文檔的方法
用XmlWriter類(lèi)實(shí)現寫(xiě)操作,該類(lèi)包含了寫(xiě)XML文檔所需的方法和屬性,它是XmlTextWriter類(lèi)和XmlNodeWriter類(lèi)的基類(lèi)。
寫(xiě)操作的有些方法是成對出現的,比如你要寫(xiě)入一個(gè)元素,首先調用WriteStartElement方法—>寫(xiě)入實(shí)際內容—>調用WriteEndElement方法結束。
下面通過(guò)其子類(lèi) XmlTextWriter 來(lái)說(shuō)明如何寫(xiě)XML文檔。
XmlTextWriter textWriter = New XmlTextWriter("C:\\myXmFile.xml", null);
在創(chuàng )建完對象后,我們調用WriterStartDocument方法開(kāi)始寫(xiě)XML文檔;
在完成寫(xiě)工作后,就調用WriteEndDocument結束寫(xiě)過(guò)程,并調用Close方法將它關(guān)閉。
在寫(xiě)的過(guò)程中,我們可以:
調用WriteComment方法來(lái)添加說(shuō)明;
通過(guò)調用WriteString方法來(lái)添加一個(gè)字符串;
通過(guò)調用WriteStartElement和WriteEndElement方法對來(lái)添加一個(gè)元素;
通過(guò)調用WriteStartAttribute和WriteEndAttribute方法對來(lái)添加一個(gè)屬性;
通過(guò)調用WriteNode方法來(lái)添加整的一個(gè)節點(diǎn);
其它的寫(xiě)的方法還包括WriteProcessingInstruction和WriteDocType等等。
下面的示例介紹如何具體運用這些方法來(lái)完成XML文檔的寫(xiě)工作。
using System;
using System.Xml;
namespace WriteXML
{
class Class1
{
static void Main( string[] args )
{
try
{
// 創(chuàng )建XmlTextWriter類(lèi)的實(shí)例對象
XmlTextWriter textWriter = new XmlTextWriter("C:\\w3sky.xml", null);
textWriter.Formatting = Formatting.Indented;
// 開(kāi)始寫(xiě)過(guò)程,調用WriteStartDocument方法
textWriter.WriteStartDocument();
// 寫(xiě)入說(shuō)明
textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
textWriter.WriteComment("w3sky.xml in root dir");
//創(chuàng )建一個(gè)節點(diǎn)
textWriter.WriteStartElement("Administrator");
textWriter.WriteElementString("Name", "formble");
textWriter.WriteElementString("site", "w3sky.com");
textWriter.WriteEndElement();
// 寫(xiě)文檔結束,調用WriteEndDocument方法
textWriter.WriteEndDocument();
// 關(guān)閉textWriter
textWriter.Close();
}
catch(System.Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
三 讀XML文檔的方法
用XmlTextReader類(lèi)的對象來(lái)讀取該XML文檔。在創(chuàng )建新對象的構造函數中指明XML文件的位置即可。
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
XmlTextReader 類(lèi)中的屬性 NodeType 可以知道其節點(diǎn)的節點(diǎn)類(lèi)型。通過(guò)與枚舉類(lèi)型 XmlNodeType 中的元素的比較,可以獲取相應節點(diǎn)的節點(diǎn)類(lèi)型并對其完成相關(guān)的操作。
枚舉類(lèi)型 XmlNodeType中包含了諸如XmlDeclaration、Attribute、CDATA、Element、Comment、Document、DocumentType、Entity、ProcessInstruction以及WhiteSpace等XML項的類(lèi)型。
下面的示例是以讀取"books.xml"文件創(chuàng )建對象,通過(guò)該xml對象的Name、BaseURI、Depth、LineNumber等屬性來(lái)獲取相關(guān)信息,并顯示在控制臺中。(運用VS.net開(kāi)發(fā)工具附帶的"books.xml"文件來(lái)作為示例)
using System;
using System.Xml;
namespace ReadXml
{
class Class1
{
static void Main( string[] args )
{
// 創(chuàng )建一個(gè)XmlTextReader類(lèi)的對象并調用Read方法來(lái)讀取XML文件
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
textReader.Read();
// 節點(diǎn)非空則執行循環(huán)體
while ( textReader.Read() )
{
// 讀取第一個(gè)元素
textReader.MoveToElement();
Console.WriteLine("XmlTextReader Properties Test");
Console.WriteLine("===================");
// 讀取該元素的屬性并顯示在控制臺中
Console.WriteLine("Name:" + textReader.Name);
Console.WriteLine("Base URI:" + textReader.BaseURI);
Console.WriteLine("Local Name:" + textReader.LocalName);
Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
Console.WriteLine("Depth:" + textReader.Depth.ToString());
Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
}
}
}
}
四 運用XmlDocument類(lèi)
XmlDocument類(lèi)代表了XML文檔,它能完成與整個(gè)XML文檔相關(guān)的各類(lèi)操作,同時(shí)和其相關(guān)的XmlDataDocument類(lèi)也是非常重要的,值得深入研究。 該類(lèi)包含了Load、LoadXml以及Save等重要的方法。
Load方法: 可以從一個(gè)字符串指定的XML文件或是一個(gè)流對象、一個(gè)TextReader對象、一個(gè)XmlReader對象導入XML數據。
LoadXml方法: 則完成從一個(gè)特定的XML文件導入XML數據的功能。
Save方法: 則將XML數據保存到一個(gè)XML文件中或是一個(gè)流對象、一個(gè)TextWriter對象、一個(gè)XmlWriter對象中。
下面的示例中,用到了XmlDocument類(lèi)對象的LoadXml方法,它從一個(gè)XML文檔段中讀取XML數據并調用其Save方法將數據保存在一個(gè)文件中。
// 創(chuàng )建一個(gè)XmlDocument類(lèi)的對象
XmlDocument doc = new XmlDocument();
doc.LoadXml(("<Student type=‘regular‘ Section=‘B‘><Name>Tommy Lex</Name></Student>"));
// 保存到文件中
doc.Save("C:\\student.xml");
// 還可以通過(guò)改變Save方法中參數,將XML數據顯示在控制臺中,方法如下:
doc.Save(Console.Out);
下面的示例中,用到了一個(gè)XmlTextReader對象,通過(guò)它讀取"books.xml"文件中的XML數據。然后創(chuàng )建一個(gè)XmlDocument對象并載入XmlTextReader對象,這樣X(jué)ML數據就被讀到XmlDocument對象中了。最后,通過(guò)該對象的Save方法將XML數據顯示在控制臺中。
XmlDocument doc = new XmlDocument();
// 創(chuàng )建一個(gè)XmlTextReader對象,讀取XML數據
XmlTextReader reader = new XmlTextReader("c:\\books.xml");
reader.Read();
// 載入XmlTextReader類(lèi)的對象
doc.Load(reader);
// 將XML數據顯示在控制臺中
doc.Save(Console.Out);
五 附錄
books.xml 文件
<?xml version=‘1.0‘?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>