"WCF Technology Insider" 23

Source: Internet
Author: User
Tags array length

WCF Technical Insider 23:2nd Part _ 5th Chapter _ News: XmlDictionaryReader and return message

XmlDictionaryReader type

XmlDictionaryReader abstract types inherit from System.Xml.XmlReader, so many XmlReader attributes are inherited. Like XmlReader, XmlDictionaryReader defines several factory methods, and they return instances of XmlDictionaryReader subtypes. Rather, XmlDictionaryReader wraps a stream and defines a number of methods that begin with read. Therefore, the use of XmlDictionaryReader and XmlReader is very similar because of the hierarchical relationship of inheritance.

Unlike XmlReader, XmlDictionaryReader is designed to read serialized and encoded XML infosets, and it is possible to reverse-process semantic compression by selectively using xmldictionary. In effect, XmlDictionaryReader and XmlDictionaryWriter are exactly the opposite, and 2 types of object models are very similar. Let's start with the XmlDictionaryReader creation method and then examine in detail how to use its Read method. Because of the similarity between XmlDictionaryReader and XmlDictionaryWriter, this section will be shorter than the one in the previous section XmlDictionaryWriter.

Create a XmlDictionaryReader object

The XmlDictionaryReader type defines several factory methods, all of which, directly or indirectly, accept a reference to a stream or byte[. Typically, the stream-oriented approach is similar to the buffer-oriented approach. For the most part, these factory methods are overloaded with 4 methods: Createdictionaryreader, Createtextreader, Createmtomreader and Createbinaryreader, Their behavior corresponds to the same name factory method as XmlDictionaryWriter. To avoid duplication, we will focus on the salient features of XmlDictionaryReader's factory approach.

Several factory methods accept references to a stream, and the other parameters used by these stream-oriented factory methods contain a reference to a Xmldictionaryquotas object and a onxmldictionaryreaderclose delegate. In all cases, the former invokes the latter, passing a null reference for the Xmldictionaryquotas and Onxmldictionaryreaderclose parameters.

The Xmldictionaryquotas type is a state container that defines important thresholds related to XML deserialization. For example, this type defines the maximum value of the node depth used in deserialization, the largest string length that is deserialized, the maximum array length of the message body, and so on "old Xu Notes 1".

The Onxmldictionaryreaderclose delegate is called when the Close method of the XmlDictionaryReader is almost over. When this delegate is activated, most of the state of the XmlDictionaryReader is set to null. Therefore, this delegate can be used as a reminder mechanism (much like an incident event), but does not provide any valuable information related to the XmlDictionaryReader state (unless NULL is valuable). The message encoder uses the Onxmldictionaryreaderclose delegate to place the XmlDictionaryReader object in the object pool. These encoders rely on reminders such as the Onxmldictionaryreaderclose delegate, which returns a XmlDictionaryReader instance in a resource pool.

The code demonstrates how to instantiate a XmlDictionaryReader object:

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 = xmldiction   Arywriter.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 = xmldiction Aryreader.createtextreader (
Stream, Encoding.bigendianunicode, New XmlDictionaryReaderQuotas (),
Delegat  e {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");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.