国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
C#中用SYSTEM.XML讀寫(xiě)XML說(shuō)明與代碼
XML是目前最常用的通用數(shù)據(jù)傳輸與處理接口類(lèi)型。本文介紹如何用C#.NET讀寫(xiě)XML文檔資料。

<?xml version="1.0" encoding="utf-8"?>
<LinkLibrary xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Link Cat="aa" Url="aa" Desc="aa" />
<Link Cat="bb" Url="aa" Desc="aa" />
<Link Cat="cc" Url="aa" Desc="aa" />
<Link Cat="aa" Url="aa" Desc="aa" />
<Link Cat="aa" Url="aa" Desc="aa" />
<Link Cat="aa" Url="aa" Desc="aa" />
<Link Cat="aa" Url="aa" Desc="aa" />
<Link Cat="aa" Url="aa" Desc="aa" />
<Link Cat="aa" Url="aa" Desc="aa" />
<Link Cat="aa" Url="aa" Desc="aa" />
</LinkLibrary>


XML概念
Root XML根節(jié)點(diǎn),只能且必須有一個(gè)(以上為L(zhǎng)inkLibrary)
Element 節(jié)點(diǎn)元素(如Link)
Attribute 節(jié)點(diǎn)屬性(如Cat, Url, Desc)
Content 內(nèi)容(非空白文本、CDATA、Element、EndElement、EntityReference 或 EndEntity)節(jié)點(diǎn)



System.Xml空間
以下類(lèi)適合快速流式讀寫(xiě)XML文件(注:DOM適合隨機(jī)讀寫(xiě))
XmlReader, XmlWriter,
XmlTextReader, XmlTextWriter
XmlValidatingReader, XmlValidatingWriter 添加了DTD和模式驗(yàn)證,因此提供了數(shù)據(jù)的有效性驗(yàn)證
XmlNodeReader, XmlNodeWriter 把XmlNode作為其源


節(jié)點(diǎn)類(lèi)型(public enum XmlNodeType)
XmlDeclaration XML 聲明(例如,<?xml version="1.0"?>)。
Attribute 屬性(例如,id="123")。
CDATA CDATA 節(jié)(例如,<![CDATA[my escaped text]]>)。
Comment 注釋?zhuān)ɡ纾?lt;!-- my comment -->)
Document 作為文檔樹(shù)的根的文檔對(duì)象提供對(duì)整個(gè) XML 文檔的訪(fǎng)問(wèn)。
DocumentFragment 文檔片段。
DocumentType 由以下標(biāo)記指示的文檔類(lèi)型聲明(例如,<!DOCTYPE...>)。
Element 元素(例如,<item>)。
EndElement 末尾元素標(biāo)記(例如,</item>)。
Entity 實(shí)體聲明(例如,<!ENTITY...>)。
EndEntity 由于調(diào)用 ResolveEntity 而使 XmlReader 到達(dá)實(shí)體替換的末尾時(shí)返回。
EntityReference 實(shí)體引用(例如,&num;)。
None 如果未調(diào)用 Read 方法,則由 XmlReader 返回。
Notation 文檔類(lèi)型聲明中的表示法(例如,<!NOTATION...>)。
ProcessingInstruction 處理指令(例如,<?pi test?>)。
SignificantWhitespace 混合內(nèi)容模型中標(biāo)記間的空白或 xml:space="preserve" 范圍內(nèi)的空白。
Text 節(jié)點(diǎn)的文本內(nèi)容。
Whitespace 標(biāo)記間的空白。



------------------------------------------------------------------------
使用XmlTextReader快速讀取
------------------------------------------------------------------------
創(chuàng)建
打開(kāi) XmlTextReader reader = new XmlTextReader("*.xml");
關(guān)閉 reader.Close();

屬性
常用
HasAttributes 獲取一個(gè)值,該值指示當(dāng)前節(jié)點(diǎn)是否有任何屬性。(從 XmlReader 繼承。)
HasValue 獲取一個(gè)值,該值指示當(dāng)前節(jié)點(diǎn)是否可以具有非 String.Empty 的 Value。
AttributeCount 獲取當(dāng)前節(jié)點(diǎn)上的屬性數(shù)。
Value 獲取當(dāng)前節(jié)點(diǎn)的文本值。
ValueType 獲取當(dāng)前節(jié)點(diǎn)的公共語(yǔ)言運(yùn)行庫(kù) (CLR) 類(lèi)型。(從 XmlReader 繼承。)
Name 獲取當(dāng)前節(jié)點(diǎn)的限定名。
其它
BaseURI 獲取當(dāng)前節(jié)點(diǎn)的基 URI。
CanReadBinaryContent 獲取一個(gè)值,該值指示 XmlTextReader 是否實(shí)現(xiàn)二進(jìn)制內(nèi)容讀取方法。
CanReadValueChunk 獲取一個(gè)值,該值指示 XmlTextReader 是否實(shí)現(xiàn) ReadValueChunk 方法。
CanResolveEntity 獲取一個(gè)值,該值指示此讀取器是否可以分析和解析實(shí)體。
Depth 獲取 XML 文檔中當(dāng)前節(jié)點(diǎn)的深度。
Encoding 獲取文檔的編碼方式。
EntityHandling 獲取或設(shè)置一個(gè)值,該值指定讀取器如何處理實(shí)體。
EOF 獲取一個(gè)值,該值指示讀取器是否定位在流的結(jié)尾。
IsDefault 獲取一個(gè)值,該值指示當(dāng)前節(jié)點(diǎn)是否是從 DTD 或架構(gòu)中定義的默認(rèn)值生成的屬性。
IsEmptyElement 獲取一個(gè)值,該值指示當(dāng)前節(jié)點(diǎn)是否為空元素(例如,<MyElement/>)。
Item 當(dāng)在派生類(lèi)中被重寫(xiě)時(shí),獲取此屬性的值。(從 XmlReader 繼承。)
LineNumber 獲取當(dāng)前行號(hào)。
LinePosition 獲取當(dāng)前行位置。
LocalName 獲取當(dāng)前節(jié)點(diǎn)的本地名稱(chēng)。
Namespaces 獲取或設(shè)置一個(gè)值,該值指示是否進(jìn)行命名空間支持。
NamespaceURI 獲取讀取器定位在的節(jié)點(diǎn)的命名空間 URI(采用 W3C 命名空間規(guī)范中定義的形式)。
NameTable 獲取與此實(shí)現(xiàn)關(guān)聯(lián)的 XmlNameTable。
NodeType 獲取當(dāng)前節(jié)點(diǎn)的類(lèi)型。
Normalization 獲取或設(shè)置一個(gè)值,該值指示是否正?;瞻缀蛯傩灾?。
Prefix 獲取與當(dāng)前節(jié)點(diǎn)關(guān)聯(lián)的命名空間前綴。
ProhibitDtd 獲取或設(shè)置一個(gè)值,該值指示是否允許 DTD 處理。
QuoteChar 獲取用于括起屬性節(jié)點(diǎn)值的引號(hào)字符。
ReadState 獲取讀取器的狀態(tài)。
SchemaInfo 獲取作為架構(gòu)驗(yàn)證結(jié)果分配給當(dāng)前節(jié)點(diǎn)的架構(gòu)信息。(從 XmlReader 繼承。)
Settings 獲取用于創(chuàng)建此 XmlTextReader 實(shí)例的 XmlReaderSettings 對(duì)象。
WhitespaceHandling 獲取或設(shè)置一個(gè)值,該值指定如何處理空白。
XmlLang 獲取當(dāng)前 xml:lang 范圍。
XmlResolver 設(shè)置用于解析 DTD 引用的 XmlResolver。
XmlSpace 獲取當(dāng)前 xml:space 范圍。

方法
定位
MoveToAttribute 移動(dòng)到指定的屬性。
MoveToContent 檢查當(dāng)前節(jié)點(diǎn)是否是內(nèi)容節(jié)點(diǎn), 如果此節(jié)點(diǎn)不是內(nèi)容節(jié)點(diǎn),則讀取器向前跳至下一個(gè)內(nèi)容節(jié)點(diǎn)或文件結(jié)尾。
MoveToElement 移動(dòng)到包含當(dāng)前屬性節(jié)點(diǎn)的元素
MoveToFirstAttribute 移動(dòng)到第一個(gè)屬性
MoveToNextAttribute 移動(dòng)到下一個(gè)屬性
Skip 跳過(guò)當(dāng)前節(jié)點(diǎn)的子級(jí)。
IsStartElement 測(cè)試當(dāng)前內(nèi)容節(jié)點(diǎn)是否是開(kāi)始標(biāo)記。

基本讀取
Read 讀取一個(gè)節(jié)點(diǎn)
ReadString 將元素或文本節(jié)點(diǎn)的內(nèi)容讀取為一個(gè)字符串。


讀取特定類(lèi)型
ReadBase64 對(duì) Base64 進(jìn)行解碼并返回解碼的二進(jìn)制字節(jié)。
ReadBinHex 對(duì) BinHex 進(jìn)行解碼并返回解碼后的二進(jìn)制字節(jié)
ReadChars 將元素的文本內(nèi)容讀入字符緩沖區(qū)。通過(guò)連續(xù)調(diào)用此方法,可以讀取大的嵌入文本流。
ReadValueChunk 讀取嵌入在 XML 文檔中的大量文本流。 (從 XmlReader 繼承。)
ReadInnerXml 將內(nèi)部XML節(jié)點(diǎn)內(nèi)容讀到一個(gè)字符串中
ReadOuterXml 所有XML節(jié)點(diǎn)內(nèi)容(包括自身節(jié)點(diǎn))

讀取節(jié)點(diǎn)
ReadStartElement 檢查當(dāng)前節(jié)點(diǎn)是否為元素并將讀取器推進(jìn)到下一個(gè)節(jié)點(diǎn)。
ReadElementString 這是一個(gè)用于讀取簡(jiǎn)單純文本元素的 Helper 方法。
ReadAttributeValue 將屬性值分析為一個(gè)或多個(gè) Text、EntityReference 或 EndEntity 節(jié)點(diǎn)
ReadEndElement 檢查當(dāng)前內(nèi)容節(jié)點(diǎn)是否為結(jié)束標(biāo)記并將讀取器推進(jìn)到下一個(gè)節(jié)點(diǎn)。
GetAttribute 獲取屬性的值。

讀取內(nèi)容并轉(zhuǎn)化
ReadContentAs... 將內(nèi)容作為指定類(lèi)型的對(duì)象讀取。 (從 XmlReader 繼承。)
ReadElementContentAs... 讀取當(dāng)前元素,并將內(nèi)容作為指定類(lèi)型的對(duì)象返回。 (從 XmlReader 繼承。)

追溯讀取
ReadToFollowing 一直讀取,直到找到具有指定限定名的元素
ReadToDescendant 讓 XmlReader 前進(jìn)到下一個(gè)匹配的子代元素。
ReadToNextSibling 讓 XmlReader 前進(jìn)到下一個(gè)匹配的同級(jí)元素。
GetRemainder 獲取已緩沖的 XML 剩余部分。
ReadSubtree 返回新的 XmlReader 實(shí)例,此實(shí)例可用于讀取當(dāng)前節(jié)點(diǎn)及其所有子節(jié)點(diǎn)。

------------------------------------------------------------------------
使用XmlTextWriter快速寫(xiě)入
------------------------------------------------------------------------
開(kāi)閉
XmlTextWriter writer = new XmlTextWriter(@"c:\mywriter.xml", null);
writer.Close();

起止XML文檔 (<?xml version="1.0"?>
writer.WriteStartDocument();
writer.EndDocument();

聲明XML格式
writer.Formatting = Formatting.Indented;
writer.Indentation = 縮進(jìn)字符數(shù)
writer.IndentChar = 縮進(jìn)字符
writer.QuoteChar = 單引號(hào)|雙引號(hào)

輸出注釋 (<!-- comment text -->)
writer.WriteComment("comment text");

輸出元素 (<Element>ElementVal</Element>)
writer.WriteElementString("Element", "ElementVal");
或者
writer.StartElement("Element");
writer.WriteString("ElementVal");
writer.EndElement();

輸出元素屬性 (<Element Property="PropertyVal">ElementVal</Element>)
writer.StartElement("Element");
writer.WriteAttributeString("Property", "PropertyVal");
writer.WriteString("ElementVal");
writer.EndElement();

輸出CDATA (<!CDATA>....</CDATA>
WriteCData("....")

輸出字符緩沖區(qū)文本
WriteChars(char[], startPos, length)



------------------------------------------------------------------------
讀例程
------------------------------------------------------------------------
XML文件
<!-- sample xml file -->
<bookstore>
<book genre="novel" ISBN="10-861003-324">
<title>The Handmaid"s Tale</title>
<price>19.95</price>
</book>
<book genre="novel" ISBN="1-861001-57-5">
<title>Pride And Prejudice</title>
<price>24.95</price>
</book>
</bookstore>


取每個(gè)書(shū)節(jié)點(diǎn)上的ISBN屬性
using (XmlReader reader = XmlReader.Create("books.xml"))
{
reader.ReadToFollowing("book");
do {
Console.WriteLine("ISBN: {0}", reader.GetAttribute("ISBN"));
} while (reader.ReadToNextSibling("book"));
}

跳到子節(jié)點(diǎn)
using (XmlReader reader = XmlReader.Create("2books.xml"))
{
reader.MoveToContent(); // bookstore
reader.ReadToDescendant("book"); // 第一個(gè)book
reader.Skip(); //Skip the first book. // 第二個(gè)book
...
}

挖到子節(jié)點(diǎn)
using (XmlReader reader = XmlReader.Create("book3.xml"))
{
reader.Read();
reader.ReadStartElement("book");
reader.ReadStartElement("title");
Console.WriteLine("The content of the title element: {0}", reader.ReadString());
reader.ReadEndElement();
reader.ReadStartElement("price");
Console.WriteLine("The content of the price element: {0}", reader.ReadString());
reader.ReadEndElement();
reader.ReadEndElement();
}



讀到指定位置
FileStream fs = new FileStream("..\\..\\..\\books.xml", FileMode.Open);
XmlTextReader tr = new XmlTextReader(fs);
while(!tr.EOF)
{
// 查找類(lèi)型為XmlNodeType.Element和名稱(chēng)為title的節(jié)點(diǎn)
if(tr.MoveToContent() == XmlNodeType.Element && tr.Name=="title")
{
listBox1.Items.Add(tr.ReadElementString());
}
else
{
tr.Read();
}
}

遍歷元素屬性
string fileName = "..\\..\\..\\books.xml";
XmlTextReader tr = new XmlTextReader(fileName);
while(tr.Read())
{
//check to see if it"s a NodeType element
if(tr.NodeType == XmlNodeType.Element)
{
//if it"s an element, then let"s look at the attributes.
for(int i = 0; i < tr.AttributeCount; i++) {
listBox1.Items.Add(tr.GetAttribute(i));
}
}


------------------------------------------------------------------------
寫(xiě)例程
------------------------------------------------------------------------
XmlTextWriter writer = new XmlTextWriter (filename, null);

//Use indenting for readability.
writer.Formatting = Formatting.Indented;

//xml聲明(Write the XML delcaration. )
writer.WriteStartDocument();

//預(yù)處理指示(Write the ProcessingInstruction node.)
String PItext="type="text/xsl" href="book.xsl"";
writer.WriteProcessingInstruction("xml-stylesheet", PItext);

//文檔類(lèi)型(Write the DocumentType node.)
writer.WriteDocType("book", null , null, "<!ENTITY h "hardcover">");

//注釋?zhuān)╓rite a Comment node.)
writer.WriteComment("sample XML");

//根元素(Write a root element.)
writer.WriteStartElement("book");

//屬性值(Write the genre attribute.)
writer.WriteAttributeString("genre", "novel");

//屬性值(Write the ISBN attribute.)
writer.WriteAttributeString("ISBN", "1-8630-014");

//Write the title.
writer.WriteElementString("title", "The Handmaid"s Tale");

//Write the style element.
writer.WriteStartElement("style");
writer.WriteEntityRef("h");
writer.WriteEndElement();

//文本元素節(jié)點(diǎn)(Write the price.)
writer.WriteElementString("price", "19.95");

//[CDATA]
writer.WriteCData("Prices 15% off!!");

//Write the close tag for the root element.
writer.WriteEndElement();

writer.WriteEndDocument();

//Write the XML to file and close the writer.
writer.Flush();
writer.Close();

//Load the file into an XmlDocument to ensure well formed XML.
XmlDocument doc = new XmlDocument();
//Preserve white space for readability.
doc.PreserveWhitespace = true;
//Load the file.
doc.Load(filename);

//Display the XML content to the console.
Console.Write(doc.InnerXml);


------------------------------------------------------------------------
帶驗(yàn)證的讀寫(xiě)
------------------------------------------------------------------------
books.xml
<?xml version="1.0"?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore xmlns="x-schema:books.xdr">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<list-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
...
</bookstore>
books.xdr
<?xml version="1.0"?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType FTEL="first-name" content="textOnly"/>
<ElementType FTEL="last-name" content="textOnly"/>
<ElementType FTEL="name" content="textOnly"/>
<ElementType FTEL="price" content="textOnly" dt:type="fixed.14.4"/>
<ElementType FTEL="author" content="eltOnly" order="one">
<group order="seq">
<element type="name"/>
</group>
<group order="seq">
<element type="first-name"/>
<element type="last-name"/>
</group>
</ElementType>
<ElementType FTEL="title" content="textOnly"/>
<AttributeType FTEL="genre" dt:type="string"/>
<ElementType FTEL="book" content="eltOnly">
<attribute type="genre" required="yes"/>
<element type="title"/>
<element type="author"/>
<element type="price"/>
</ElementType>
<ElementType FTEL="bookstore" content="eltOnly">
<element type="book"/>
</ElementType>
</Schema>
cs
using System.Xml.Schema;
protected void button1_Click (object sender, System.EventArgs e)
{
//change this to match your path structure.
string fileName = "..\\..\\..\\booksVal.xml";
XmlTextReader tr = new XmlTextReader(fileName);
XmlValidatingReader trv = new XmlValidatingReader(tr);

//Set validation type
trv.ValidationType=ValidationType.XDR;
//Add in the Validation eventhandler
trv.ValidationEventHandler += new ValidationEventHandler(this.ValidationEvent);

//Read in node at a time
while(trv.Read())
{
if(trv.NodeType == XmlNodeType.Text)
listBox1.Items.Add(trv.Value);
}
}
public void ValidationEvent (object sender, ValidationEventArgs args)
{
MessageBox.Show(args.Message);
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
用C#讀xml文檔(流模型)
XmlTextReader 類(lèi) (System.Xml)
Windows8XLinq/XmlReader/Writer數(shù)據(jù)消息查詢(xún)
在.NET Framework中輕松處理XML數(shù)據(jù)
HOW TO:使用 Visual C
dom4j生成xml實(shí)例
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服