軟件測試開發技術.Net中的XmlReader與XmlWriter解析[2]

發表于:2009-08-12來源:作者:點擊數: 標簽:軟件測試XmlWriterXmlReaderNetNET
軟件測試 開發 技術.Net中的XmlReader與XmlWriter解析[2] .Net網站架構 關鍵字:.Net XmlReader XmlWriter 檢索屬性數據 AttributeCountry屬性確定屬性個數。GetAttribute()方法按照名稱或索引來獲取屬性,如果要一次迭代一個屬性就可以使用MoveToFirstAttri

軟件測試開發技術.Net中的XmlReader與XmlWriter解析[2]  .Net網站架構

關鍵字:.Net XmlReader XmlWriter

  檢索屬性數據

  AttributeCountry屬性確定屬性個數。GetAttribute()方法按照名稱或索引來獲取屬性,如果要一次迭代一個屬性就可以使用MoveToFirstAttribute()和MoveToNextAttribute()方法。

  richTextBox1.Clear();

  XmlReader tr = XmlReader.Create("book.xml");

  while (tr.Read()){

  if (tr.NodeType == XmlNodeType.Element){

  for (int i = 0; i < tr.AttributeCount; i++){

  richTextBox1.AppendText(tr.GetAttribute(i)+"\r\n");

  }

  }

  }

  使用XmlReader類進行驗證

  有時不但要知道文檔的格式是規范的,還是確定文檔是有效的。

  XmlReader可以使用XmlReaderSettings,根據XSD模式驗證XML。XSD模式添加到XMLSchemaSet中,通過Schema屬性可以訪問XMLSchemaSet。XsdValidate屬性還必須設置為ture,這個屬性默認為flase.

  XmlWriter類可以把Xml寫入一個流、文件、StringBuilder,TextWriter或另一個XmlWriter對象中。與XmlReader一樣,XmlWriter類以只向前、未緩存的方式 進行寫入。

  使用XmlWirterSettings對旬進行是否縮進文本、縮進量等配置。

  XmlWriterSettings settings = new XmlWriterSettings();

  settings.Indent = true; //是否縮進

  settings.NewLineOnAttributes = true;//把每個屬性寫在一行,這樣做可以更容易讀取

  XMLXmlWriter writer = XmlWriter.Create("booknew.xml",settings);

  writer.WriteStartDocument();

  writer.WriteStartElement("book");

  writer.WriteAttributeString("genre","Mystery");

  writer.WriteAttributeString("publicationdate","2001");

  writer.WriteAttributeString("ISBN","123456489");

  writer.WriteElementString("title","Case of the Money");

  writer.WriteStartElement("author");

  writer.WriteElementString("name","Cookie Monster");

  writer.WriteEndElement();

  writer.WriteElementString("price","9.99");

  writer.WriteEndDocument();

  writer.Flush();

  writer.Close();

  ·使用XmlWriterSettings實例對象進行生成的XML的設置。

  ·使用Create(),返回一個XmlWriter對象,其中Create(),第一個參數為Xml的名字,第二個參數為XmlWriterSettings實例對象。

  ·使用WriterStartDocument()中文檔聲明,開始寫入數據,以WriteEndDocument()結束。注間控制元素的嵌套,注注意WriterStartElement()和WriterEndElement()的調用與位置。

  ·還有一些專用的寫入方法。WriteCData()可以輸出一個CData部分(),WriterComment()以正確的XML格式寫入注釋。WriteChae()寫入字符緩沖區的內容。

 

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97