WCF Technical Insider 22:2nd Part _ 5th Chapter _ News: XmlDictionaryWriter

Source: Internet
Author: User

(Overview: This section describes the concepts of XmlDictionaryWriter types: How to create, use an object, and then describe how to do text, binary, and Mtom encoding.) Finally, it introduces the function of Xmldictionary "old Xu Remark 2", which has been the problem of various coding efficiency. You will learn the basics of several coding formats supported for WCF. )

XmlDictionaryWriter

The XmlDictionaryWriter type is serialized and encoded for the message type. It inherits from System.Xml.XmlWriter, so it inherits many of the XmlWriter properties. Like XmlWriter, XmlDictionaryWriter is also an abstract type that defines several ways to return instances of XmlDictionaryWriter subtypes. System.IO.Stream is packaged and defines a number of ways to start with a write word. In effect, the use of XmlDictionaryWriter and XmlWriter in the program is very similar in concept.

Unlike XmlWriter, the purpose of the XmlDictionaryWriter type is to serialize and encode the message object, and sometimes use the Xmldictionary object to handle the compression work. For this purpose, the XmlDictionaryWriter type defines a number of members that are different from the XmlWriter. Let's look at these members to learn more about the XmlDictionaryWriter type. First we examine the XmlDictionaryWriter constructor, and then see how to serialize and encode the XML data through the stream.

Create a XmlDictionaryWriter object

XmlDictionaryWriter defines several factory methods, and they both directly or indirectly accept references to System.IO.Stream objects. Most of these methods are overloaded with 4 methods: Createdictionarywriter, CreateTextWriter, Createmtomwriter, and Createbinarywriter.

Createdictionarywriter the factory method on the XmlDictionaryWriter type Createdictionarywriter one of them is to accept a reference of a XmlWriter type. Internally, the instances returned by these methods are simply packaged with the passed parameter XmlWriter. Therefore, these methods are of little use, except in some APIs where XmlDictionaryWriter types are required. For example, you might want to drop it. A method that accepts XmlDictionaryWriter type parameters, but you have only one XmlWriter type of local variable. If so, you can create XmlDictionaryWriter objects from XmlWriter by calling the Createdictionarywriter factory method, passing the XmlWriter argument, and the code is as follows:

MemoryStream stream = new MemoryStream();
XmlWriter xmlWriter = XmlWriter.Create(stream);
XmlDictionaryWriter writer =  XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);

The

CreateTextWriter XmlDictionaryWriter type defines three factory methods. These factory methods return instances that inherit from the XmlDictionaryWriter type, and they function to produce text-based XML. All three methods accept the parameters of the stream type. Two methods accept a stream parameter and a parameter of a System.Text.Encoding type. A method accepts a stream type, a encoding, and a Boolean type of argument. Encoding the parameters, as you would expect, to set the encoding format when the stream is processed. Although there are many encoding formats, the CreateTextWriter method supports only three encoding formats: UTF-8, Unicode (UTF-16) Little-endian, and Big-endian. If not selected, the UTF-8 encoding is used by default. The Boolean parameter indicates whether XmlDictionaryWriter owns this stream object. If true, the close and Dispose methods on the XmlDictionaryWriter are invoked, and the method of the stream object is called, so that subsequent access to the stream is blocked. If it is not set, the default is true. The following code shows how to use the CreateTextWriter method:

MemoryStream stream = new MemoryStream();
using (XmlDictionaryWriter writer =
  XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8,  false)) {
       writer.WriteStartDocument();
       writer.WriteElementString("SongName",
                                 "urn:ContosoRockabilia",
                                 "Aqualung");
       writer.Flush();
}

Console.WriteLine("XmlDictionaryWriter (Text-UTF8) wrote {0}  bytes",
                   stream.Position);
  stream.Position = 0;
  Byte[] bytes = stream.ToArray();
  Console.WriteLine(BitConverter.ToString(bytes));
  Console.WriteLine("data read from stream:\n{0}\n",
     new StreamReader(stream).ReadToEnd());

When the code is running, it produces the following output:

XmlDictionaryWriter (Text-UTF8) wrote 97 bytes3C-3F-78- 6D-6C-20-76-65-72-73-69-6F-6E-3D-22-31-2E-30-22-20-65-6E-63-6F-64-69- 6E-67-3D-2275-74-66-2D-38-22-3F-3E-3C-53-6F-6E-67-4E-61-6D-65-20-78- 6D-6C-6E-73-3D-22-75-72-6E-3A-436F-6E-74-6F-73-6F-52-6F-63-6B-61-62- 69-6C-69-61-22-3E-41-71-75-61-6C-75-6E-67-3C-2F-53-6F6E-67-4E-61-6D- 65-3E
data read from stream:
<?xml version="1.0" encoding="utf-8"?>
<SongName  xmlns="urn:ContosoRockabilia">Aqualung</SongName>

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.