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

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
如何完成.Net下XML文檔的讀寫操作

如何完成.Net下XML文檔的讀寫操作

  轉(zhuǎn)自: xugang - 博客園


一  .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 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>

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
讀取XML文件
XmlDocument 類 (System.Xml)
xml
HOW TO:使用 Visual C
用XML結(jié)點的屬性值查找相同結(jié)點的其它屬性值[C#]
C#使用XmlDocument操作XML進行查詢、增加、修改、刪除、保存應(yīng)用的實例(轉(zhuǎn)載)...
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服