《WCF技術內幕》23

來源:互聯網
上載者:User

《WCF技術內幕》23:第2部分_第5章_訊息:XmlDictionaryReader和回到Message

XmlDictionaryReader類型

XmlDictionaryReader抽象類別型繼承自System.Xml.XmlReader,因此繼承了很 多XmlReader的特性。和XmlReader 一樣,XmlDictionaryReader定義了幾個工廠 方法,他們返回的是XmlDictionaryReader的子類型的執行個體。更確切地說, XmlDictionaryReader封裝了一個Stream,並定義了許多以Read開頭的方法。因 此,由於繼承的層次關係,使用 XmlDictionaryReader和XmlReader很相似。

和XmlReader不同,XmlDictionaryReader的目的是為了讀取序列化的和編碼 過的XML Infosets ,並且可以由選擇地藉助XmlDictionary來實現語義壓縮的反 向處理。作用上,XmlDictionaryReader 和XmlDictionaryWriter正好相反,並 且2個類型的物件模型非常相似。讓我們從XmlDictionaryReader的建立方法開始 學習,然後詳細研究如何使用它的Read方法。因為XmlDictionaryReader和 XmlDictionaryWriter的相似性,本節會比上一節XmlDictionaryWriter要簡短一 些。

建立一個XmlDictionaryReader對象

XmlDictionaryReader類型定義了幾個Factory 方法,所有這些方法都直接或者間 接地,接受一個Stream 或Byte[]的引用。通常來說,面向stream的方法與面向 buffer的方法很相似。絕大部分,這些Factory 方法都是重載一下4個方法: CreateDictionaryReader, CreateTextReader, CreateMtomReader和 CreateBinaryReader,它們的行為與XmlDictionaryWriter的相同名字Factory 方法 對應。為了避免重複,我們將會關注XmlDictionaryReader的Factory 方法的顯著特 性上。

幾個Factory 方法接受一個Stream的引用,這些面向stream的Factory 方法使用的其 它參數包含一個XmlDictionaryQuotas 對象的引用和一個 OnXmlDictionaryReaderClose委託。在所有的情況下,前者調用後者,為 XmlDictionaryQuotas 和OnXmlDictionaryReaderClose參數傳遞null引用。

XmlDictionaryQuotas類型是一個狀態容器,它用來定義與XML還原序列化相關 的重要的閥值。例如,這個類型定義還原序列化中用到的節點深度的最大值、反序 列化最大的String長度、訊息體的最大數組長度等等【老徐備忘1】。

OnXmlDictionaryReaderClose委託在XmlDictionaryReader 的Close方法幾乎 結束的時候調用。當這個委託被啟用以後,XmlDictionaryReader的大部分狀態 被設定為null。因此,這個委託可以用來作為提醒機制(很像一個事件event) ,但不會提供XmlDictionaryReader狀態相關的任何有價值的資訊(除非Null是 有價值的)。Message編碼器使用OnXmlDictionaryReaderClose委託去把 XmlDictionaryReader對象放到對象池。這些編碼器依賴 OnXmlDictionaryReaderClose委託這樣的提醒,它會返回一個資源集區裡的 XmlDictionaryReader執行個體。

一下代碼示範了如何執行個體化一個XmlDictionaryReader對象:

private static void CreateTextReader() {
  Console.WriteLine("==== Creating XML Dictionary Text  Reader ====");
  MemoryStream stream = new MemoryStream();

  // create an XmlDictionaryWriter and serialize/encode some  XML
  XmlDictionaryWriter writer =  XmlDictionaryWriter.CreateTextWriter(stream,
     Encoding.BigEndianUnicode, false);
  writer.WriteStartDocument();
     writer.WriteElementString("SongName",
                               "urn:ContosoRockabilia",
                               "Aqualung");
  writer.Flush();
  stream.Position = 0;

  // create an XmlDictionaryReader to decode/deserialize the  XML
  XmlDictionaryReader reader =  XmlDictionaryReader.CreateTextReader(
     stream, Encoding.BigEndianUnicode, new  XmlDictionaryReaderQuotas(),
     delegate { Console.WriteLine("closing reader"); }  );
  reader.MoveToContent();
  Console.WriteLine("Read XML Content:{0}",reader.ReadOuterXml ());

  Console.WriteLine("about to call reader.Close()");
  reader.Close();
  Console.WriteLine("reader closed");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.