Easy XML data processing in. NETFramework (4-3)

Source: Internet
Author: User
?? The code In demonstrates converting a string data to a Base64 encoded XML stream. Is the output result. Figure8PersistingaStringArrayasBase64usingSystem; usingSystem. Text; usingSystem. IO; usingSystem. Xm ?? The code In demonstrates converting a string data to a Base64 encoded XML stream. Is the output result.


Figure 8 Persisting a String Array as Base64

Using System;

Using System. Text;

Using System. IO;

Using System. Xml;



Class MyBase64Array

{

Public static void Main (String [] args)

{

String outputFileName = 'test64. XML ';

If (args. Length> 0)

OutputFileName = args [0]; // file name



// Convert the array into XML

String [] theArray = {'Rome ', 'New York', 'sydonder', 'stockhol ',

'Paris '};



CreateOutput (theArray, outputFileName );

Return;

}



Private static void CreateOutput (string [] theArray, string filename)

{

// Open XML writer

XmlTextWriter xmlw = new XmlTextWriter (filename, null );

// Enable the subelement to be indented based on Indentation and IndentChar. This option only indent the element content

Xmlw. Formatting = Formatting. Indented;

// Write the XML declaration with the version "1.0"

Xmlw. WriteStartDocument ();

// Write comments containing the specified text .

Xmlw. WriteComment ('Array to Base64 XML ');

// Start writing an array node

Xmlw. WriteStartElement ('array ');

// Write out attributes with the specified prefix, local name, namespace URI, and value

Xmlw. WriteAttributeString ('xmls', 'X', null, 'dinoe: msdn-mag ');

// Cyclically write the child node into the array

Foreach (string s in theArray)

{

// Write the specified start mark and associate it with the given namespace and prefix.

Xmlw. WriteStartElement ('X', 'element', null );

// Convert S to byte [] array, encode byte [] array into Base64, and write the result text. the number of bytes to be written is twice the total length of s, A string occupies 2 bytes.

Xmlw. WriteBase64 (Encoding. Unicode. GetBytes (s), 0, s. Length * 2 );

// Close the subnode

Xmlw. WriteEndElement ();

}

// Close the root node, with only two levels

Xmlw. WriteEndDocument ();



// Close the writer

Xmlw. Close ();



// Read the written content

XmlTextReader reader = new XmlTextReader (filname );

While (reader. Read ())

{

// Obtain the node named element

If (reader. LocalName = 'element ')

{



Byte [] bytes = new byte [1, 1000];

Int n = reader. ReadBase64 (bytes, 0, 1000 );

String buf = Encoding. Unicode. GetString (bytes );



Console. WriteLine (buf. Substring (0, n ));

}

}

Reader. Close ();



}

}



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.