這篇文章將為大家詳細講解有關(guān).net如何讀寫xml文檔,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)專注于青縣網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供青縣營銷型網(wǎng)站建設(shè),青縣網(wǎng)站制作、青縣網(wǎng)頁設(shè)計、青縣網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造青縣網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供青縣網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
一 .Net框架中與XML有關(guān)的命名空間
System.Xml
包含了一些和XML文檔的讀寫操作相關(guān)的類,它們分別是:XmlReader、XmlTextReader、XmlValidatingReader、XmlNodeReader、XmlWriter、XmlTextWriter 以及 XmlNode(它的子類包括:XmlDocument、XmlDataDocument、XmlDocumentFragment)等類。
System.Xml.Schema
包含了和XML模式相關(guān)的類,這些類包括XmlSchema、XmlSchemaAll、XmlSchemaXPath以及XmlSchemaType等類。
System.Xml.Serialization
包含了和XML文檔的序列化和反序列化操作相關(guān)的類。
序列化:將XML格式的數(shù)據(jù)轉(zhuǎn)化為流格式的數(shù)據(jù),并能在網(wǎng)絡(luò)中傳輸;
反序列化:完成相反的操作,即將流格式的數(shù)據(jù)還原成XML格式的數(shù)據(jù)。
System.Xml.Xpath
包含了XPathDocument、XPathExression、XPathNavigator以及XPathNodeIterator等類,這些類能完成XML文檔的導(dǎo)航功能。
(在XPathDocument類的協(xié)助下,XPathNavigator類能完成快速的XML文檔導(dǎo)航功能,該類為程序員提供了許多Move方法以完成導(dǎo)航功能。)
System.Xml.Xsl
完成XSLT的轉(zhuǎn)換功能。
二 寫XML文檔的方法
用XmlWriter類實現(xiàn)寫操作,該類包含了寫XML文檔所需的方法和屬性,它是XmlTextWriter類和XmlNodeWriter類的基類。
寫操作的有些方法是成對出現(xiàn)的,比如你要寫入一個元素,首先調(diào)用WriteStartElement方法—>寫入實際內(nèi)容—>調(diào)用WriteEndElement方法結(jié)束。
下面通過其子類 XmlTextWriter 來說明如何寫XML文檔。
XmlTextWriter textWriter = New XmlTextWriter("C:\\myXmFile.xml", null);
在創(chuàng)建完對象后,我們調(diào)用WriterStartDocument方法開始寫XML文檔;
在完成寫工作后,就調(diào)用WriteEndDocument結(jié)束寫過程,并調(diào)用Close方法將它關(guān)閉。
在寫的過程中,我們可以:
調(diào)用WriteComment方法來添加說明;
通過調(diào)用WriteString方法來添加一個字符串;
通過調(diào)用WriteStartElement和WriteEndElement方法對來添加一個元素;
通過調(diào)用WriteStartAttribute和WriteEndAttribute方法對來添加一個屬性;
通過調(diào)用WriteNode方法來添加整的一個節(jié)點;
其它的寫的方法還包括WriteProcessingInstruction和WriteDocType等等。
下面的示例介紹如何具體運用這些方法來完成XML文檔的寫工作。
代碼如下:
using System;
using System.Xml;
namespace WriteXML
{
class Class1
{
static void Main( string[] args )
{
try
{
// 創(chuàng)建XmlTextWriter類的實例對象
XmlTextWriter textWriter = new XmlTextWriter("C:\\w3sky.xml", null);
textWriter.Formatting = Formatting.Indented;
// 開始寫過程,調(diào)用WriteStartDocument方法
textWriter.WriteStartDocument();
// 寫入說明
textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
textWriter.WriteComment("w3sky.xml in root dir");
//創(chuàng)建一個節(jié)點
textWriter.WriteStartElement("Administrator");
textWriter.WriteElementString("Name", "formble");
textWriter.WriteElementString("site", "w3sky.com");
textWriter.WriteEndElement();
// 寫文檔結(jié)束,調(diào)用WriteEndDocument方法
textWriter.WriteEndDocument();
// 關(guān)閉textWriter
textWriter.Close();
}
catch(System.Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}三 讀XML文檔的方法
用XmlTextReader類的對象來讀取該XML文檔。在創(chuàng)建新對象的構(gòu)造函數(shù)中指明XML文件的位置即可。
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
XmlTextReader 類中的屬性 NodeType 可以知道其節(jié)點的節(jié)點類型。通過與枚舉類型 XmlNodeType 中的元素的比較,可以獲取相應(yīng)節(jié)點的節(jié)點類型并對其完成相關(guān)的操作。
枚舉類型 XmlNodeType 中包含了諸如XmlDeclaration、Attribute、CDATA、Element、Comment、Document、DocumentType、Entity、ProcessInstruction以及WhiteSpace等XML項的類型。
下面的示例是以讀取"books.xml"文件創(chuàng)建對象,通過該xml對象的Name、BaseURI、Depth、LineNumber等屬性來獲取相關(guān)信息,并顯示在控制臺中。(運用VS.net開發(fā)工具附帶的"books.xml"文件來作為示例)
代碼如下:
using System;
using System.Xml;
namespace ReadXml
{
class Class1
{
static void Main( string[] args )
{
// 創(chuàng)建一個XmlTextReader類的對象并調(diào)用Read方法來讀取XML文件
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
textReader.Read();
// 節(jié)點非空則執(zhí)行循環(huán)體
while ( textReader.Read() )
{
// 讀取第一個元素
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類
XmlDocument類代表了XML文檔,它能完成與整個XML文檔相關(guān)的各類操作,同時和其相關(guān)的XmlDataDocument類也是非常重要的,值得深入研究。 該類包含了Load、LoadXml以及Save等重要的方法。
Load方法: 可以從一個字符串指定的XML文件或是一個流對象、一個TextReader對象、一個XmlReader對象導(dǎo)入XML數(shù)據(jù)。
LoadXml方法: 則完成從一個特定的XML文件導(dǎo)入XML數(shù)據(jù)的功能。
Save方法: 則將XML數(shù)據(jù)保存到一個XML文件中或是一個流對象、一個TextWriter對象、一個XmlWriter對象中。
下面的示例中,用到了XmlDocument類對象的LoadXml方法,它從一個XML文檔段中讀取XML數(shù)據(jù)并調(diào)用其Save方法將數(shù)據(jù)保存在一個文件中。
代碼如下:
// 創(chuàng)建一個XmlDocument類的對象
XmlDocument doc = new XmlDocument();
doc.LoadXml(("<Student type='regular' Section='B'><Name>Tommy Lex</Name></Student>"));
// 保存到文件中
doc.Save("C:\\student.xml");
// 還可以通過改變Save方法中參數(shù),將XML數(shù)據(jù)顯示在控制臺中,方法如下:
doc.Save(Console.Out);
下面的示例中,用到了一個XmlTextReader對象,通過它讀取"books.xml"文件中的XML數(shù)據(jù)。然后創(chuàng)建一個XmlDocument對象并載入XmlTextReader對象,這樣XML數(shù)據(jù)就被讀到XmlDocument對象中了。最后,通過該對象的Save方法將XML數(shù)據(jù)顯示在控制臺中。
XmlDocument doc = new XmlDocument();
// 創(chuàng)建一個XmlTextReader對象,讀取XML數(shù)據(jù)
XmlTextReader reader = new XmlTextReader("c:\\books.xml");
reader.Read();
// 載入XmlTextReader類的對象
doc.Load(reader);
// 將XML數(shù)據(jù)顯示在控制臺中
doc.Save(Console.Out);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>
另外一個.net操作xml文件示例
代碼如下:
//設(shè)置配置文件物理路徑
public string xmlPath = "/manage/spider/config.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//設(shè)置程序物理路徑+文件物理路徑
string path = Request.PhysicalApplicationPath + xmlPath;
//獲取XML元素對象
XElement config = XElement.Load(path);
if (config != null)
{
//獲得節(jié)點子元素
XElement eleAmazonDetailUrl = config.Element("AmazonDetailUrl");
XElement eleAmazonListUrl = config.Element("AmazonListUrl");
XElement eleHz = config.Element("Hz");
XElement eleCount = config.Element("Count");
//在頁面上呈現(xiàn)取到的數(shù)據(jù)
if (eleAmazonDetailUrl != null)
TextBox_AmazonDetailUrl.Text = eleAmazonDetailUrl.Value;
if (eleAmazonListUrl != null)
TextBox_AmazonListUrl.Text = eleAmazonListUrl.Value;
if (eleHz != null)
TextBox_Hz.Text = eleHz.Value;
if (eleCount != null)
TextBox_Count.Text = eleCount.Value;
}
else
Response.Write("");
}
}
protected void btn_Save_Click(object sender, EventArgs e)
{
//設(shè)置XML文件路徑
string path = Request.PhysicalApplicationPath + xmlPath;
//設(shè)置節(jié)點的名稱和內(nèi)容
XElement root = new XElement("Settings",
new XElement("AmazonDetailUrl", TextBox_AmazonDetailUrl.Text.Trim()),
new XElement("AmazonListUrl", TextBox_AmazonListUrl.Text.Trim()),
new XElement("Hz", TextBox_Hz.Text.Trim()),
new XElement("Count", TextBox_Count.Text.Trim())
);
//將元素序列化到指定路徑的XML文件當(dāng)中
root.Save(path);
}關(guān)于“.net如何讀寫xml文檔”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
名稱欄目:.net如何讀寫xml文檔
分享地址:http://chinadenli.net/article28/ppchcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、外貿(mào)網(wǎng)站建設(shè)、電子商務(wù)、小程序開發(fā)、網(wǎng)站內(nèi)鏈、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)