In quite a few cases, we write XML files by default in text format, if the XML content is transmitted over the network, or if you want to save space, especially for large XML documents, consider compressing the size of the XML file as much as possible.
The XmlDictionaryWriter class derives from the XmlWriter class, which exposes a number of static methods that can directly create XML-written objects based on text, binary, and mtom formats. Text format is not much to say, is directly to the XML content written to the file, binary and Mtom similar to the file content compression, plainly, is the output as a pure byte form. Mtom are often used to read and write SOAP messages.
We can do an example to compare the difference between writing XML in text and writing XML in binary mode.
First, you define an enumeration that determines whether to write in a text format or in a binary format.
Public enum Writeformat { Text, Binary }
Then write a Savexmltofile method, the first parameter is the path to the XML file to be saved, and the second parameter is the enumeration just defined to determine the write format.
Public voidSavexmltofile (stringfilepath, Writeformat FMT) { using(FileStream fs =NewFileStream (filepath, FileMode.OpenOrCreate, FileAccess.Write)) { //Create writerXmlDictionaryWriter writer =NULL; if(FMT = =Writeformat.text) Writer=Xmldictionarywriter.createtextwriter (FS); Elsewriter=Xmldictionarywriter.createbinarywriter (FS); //Write Document Declarationwriter. WriteStartDocument (); //write to root nodeWriter. WriteStartElement ("Pets"); //Write to pet child nodeWriter. WriteStartElement ("name");//< name >Writer. WriteString ("Dutch Rabbits"); Writer. WriteEndElement (); //</name >Writer. WriteStartElement ("Age");//< Age >Writer. WriteString ("2"); Writer. WriteEndElement (); //</Age >//Direct Flushwriter. Flush (); Writer. Dispose (); } }
Directly call XmlDictionaryWriter Createxxxwriter method, you can get a xmldictionarywriter instance, xxx depends on the format you want to use, there are text, Binary, mtom three kinds.
When writing to XML in a non-textual format, the text content in XML is stored in an object called Xmldictionary to hold the string map, and the Xmldictionary object can be specified manually. Each string instance that is stored in it can be wrapped with an xmldictionarystring instance.
If we do not specify a Xmldictionary instance, writer automatically creates and places all the string objects in the XML document (namespace, element name, attribute name, and content of the text node) into the xmldictionary and maps it to binary content.
If there is nothing to write about, you can call the Flush method directly, which not only writes the buffered content to the stream, but also automatically complements the end tag of the XML element.
The XML file is now written in text format and binary format, respectively. The Doc1.xml file is written in text format, Doc2.xml is written in binary format, and the results are as follows.
The same content, saved in text format, requires 99 bytes, while using binary format requires 36 bytes.
Open an XML document saved in binary format with Notepad, and you will see something like this.
Open the file saved in text format, you can see the content directly.
All right, here's the crap for today, it's dinner.
". Net deep breathing" uses binary format to compress XML documents