C#温故而知新学习系列之XML编程—Xml写入器XmlWriter类(三)
前言
? 與XmlReader類(lèi)相對(duì)應(yīng),.NETFramework也提供了一個(gè)以快速的,非緩存的,只向前的,動(dòng)態(tài)寫(xiě)入XML數(shù)據(jù)的類(lèi)即XmlWriter類(lèi),可以將XmlWriter類(lèi)理解為與XmlReader類(lèi)對(duì)等的類(lèi)
閱讀目錄
一:寫(xiě)入XML文檔的步驟
二:實(shí)現(xiàn)步驟
?? 一:寫(xiě)入XML文檔的步驟
為了寫(xiě)入屬性,元素,需要調(diào)用一個(gè)WriteStrat某某()方法和WriteEnd()某某方法,在使用XmlWriter類(lèi)的時(shí)候并不是簡(jiǎn)單的寫(xiě)入一個(gè)元素,需要先寫(xiě)入開(kāi)始標(biāo)簽,然后寫(xiě)入內(nèi)容,最后寫(xiě)入結(jié)束標(biāo)簽,因此必須要跟蹤在XML文檔中所處的位置,以保證在正確的時(shí)間調(diào)用正確的結(jié)束方法
1:使用XmlWriter類(lèi)的Create()方法創(chuàng)建該類(lèi)的實(shí)例,并將XML文檔名稱(chēng)作為參數(shù)傳入方法
2:開(kāi)始文檔
3:寫(xiě)入開(kāi)始標(biāo)簽
4:寫(xiě)入內(nèi)容
5:寫(xiě)入結(jié)束標(biāo)簽
6:結(jié)束文檔;
實(shí)例?
二:實(shí)現(xiàn)步驟
1:XML文件編寫(xiě)
? <?xml version="1.0" encoding="utf-8" ?>
2:代碼文件編寫(xiě)
Form1.cs
? using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace XmlWriterClass
{
??? public partial class Form1 : Form
??? {
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????? }
}
??????? private void Form1_Load(object sender, EventArgs e)
??????? {
??????????? XmlWriter xml_doc = XmlWriter.Create("XmlFile.xml");
??????????? ?? //第一種寫(xiě)入元素方法
??????????? //寫(xiě)版本為“1.0”的XML聲明
??????????? xml_doc.WriteStartDocument();
??????????? //寫(xiě)入元素的名稱(chēng)以及它的值,注意這種寫(xiě)入元素的方法,是不能在其上面寫(xiě)入任何屬性的
??????????? xml_doc.WriteElementString("Title", "C#教程");
??????????? //關(guān)閉文檔
??????????? xml_doc.WriteEndDocument();
??????????? xml_doc.Flush();
??????????? xml_doc.Close();?
?//第二種寫(xiě)入元素方法
??????????? //寫(xiě)版本為“1.0”的XML聲明
??????????? xml_doc.WriteStartDocument();
??????????? //寫(xiě)指定的開(kāi)始標(biāo)記,這種寫(xiě)入元素的方法,是可以在其上面寫(xiě)入屬性的
??????????? xml_doc.WriteStartElement("Books");
??????????? //關(guān)閉元素
??????????? xml_doc.WriteEndElement();
??????????? //關(guān)閉文檔
??????????? xml_doc.WriteEndDocument();
??????????? xml_doc.Flush();
??????????? xml_doc.Close();
//第一種寫(xiě)入屬性方法
??????????? xml_doc.WriteStartDocument();
?//寫(xiě)入<Books/>元素
??????????? xml_doc.WriteStartElement("Books");
//寫(xiě)入屬性的名稱(chēng)
??????????? xml_doc.WriteStartAttribute("issue");
//寫(xiě)入屬性的值
??????????? xml_doc.WriteValue("清華出版社");
?//關(guān)閉上一個(gè)xml_doc.WriteStartAttribute("issue")的 調(diào)用
??????????? xml_doc.WriteEndAttribute();
?//關(guān)閉<Books/>元素
??????????? xml_doc.WriteEndElement();
??????????? xml_doc.WriteEndDocument();
??????????? xml_doc.Flush();
??????????? xml_doc.Close();
//第二種寫(xiě)入屬性方法
?xml_doc.WriteStartDocument();
??????????? xml_doc.WriteStartElement("Books");
//一次性寫(xiě)入屬性的名稱(chēng)和值
??????????? xml_doc.WriteAttributeString("issue", "清華出版社");
??????????? xml_doc.WriteEndElement();
??????????? xml_doc.WriteEndDocument();
??????????? xml_doc.Flush();
??????????? xml_doc.Close();
//結(jié)合使用
? xml_doc.WriteStartDocument();
??????????? //寫(xiě)入<Books/>元素和屬性值,在這里我們不采用xml_doc.WriteElementString()方法來(lái)寫(xiě)入元素,因?yàn)槲覀冞€要在我們寫(xiě)入的元素上面寫(xiě)入屬性,而xml_doc.WriteElementString()這種寫(xiě)入元素的方法,是不能在其上面寫(xiě)入任何屬性的
??????????? xml_doc.WriteStartElement("Books");
??????????? xml_doc.WriteAttributeString("issue", "清華出版社");
??????????? //寫(xiě)入<Titles/>元素和屬性值,注意我們這里用了另外一種寫(xiě)入屬性的方法
??????????? xml_doc.WriteStartElement("Title");
??????????? xml_doc.WriteStartAttribute("OthorName");
??????????? xml_doc.WriteValue("C#入門(mén)與精通");
??????????? xml_doc.WriteEndAttribute();
??????????? xml_doc.WriteValue("C#基礎(chǔ)");
??????????? xml_doc.WriteEndElement();
??????????? xml_doc.WriteEndElement();
??????????? xml_doc.WriteEndDocument();
??????????? xml_doc.Flush();
??????????? xml_doc.Close();
}
}
總結(jié)
以上是生活随笔為你收集整理的C#温故而知新学习系列之XML编程—Xml写入器XmlWriter类(三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: base与this
- 下一篇: 【转载】C# ??(问问,问号问号)运算