Easy processing of XML data in the. NET Framework (4-3)

Source: Internet
Author: User
?? The code in Figure VIII shows an XML stream that converts a string of data to BASE64 encoding. Figure IX is the result of the output.


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 an array to XML

String[] TheArray = {' Rome ', ' New York ', ' Sydney ', ' Stockholm ',

' Paris '};



Createoutput (TheArray, outputfilename);

Return

}



private static void Createoutput (string[] thearray, string filename)

{

Open XML writer

XmlTextWriter XMLW = new XmlTextWriter (filename, null);

Causes child elements to set indents based on indentation and IndentChar. This option indents only the element content

XMLW. formatting = formatting.indented;

Write XML declaration with version "1.0"

XMLW. WriteStartDocument ();

Writes out a comment that contains the specified text.

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

Start writing out an array node

XMLW. WriteStartElement (' array ');

Writes out a property with the specified prefix, local name, namespace URI, and value

XMLW. WriteAttributeString (' xmlns ', ' x ', null, ' Dinoe:msdn-mag ');

Looping a child node into an array

foreach (string s in TheArray)

{

Writes out the specified start tag and associates it with the given namespace and prefix.

XMLW. WriteStartElement (' x ', ' element ', NULL);

Convert s to byte[] array and encode the byte[] array to Base64 and write the result text, the number of bytes to write into is twice times the total length of s, and a string is 2 bytes.

XMLW. WriteBase64 (Encoding.Unicode.GetBytes (s), 0, s.length*2);

Enclosing child nodes

XMLW. WriteEndElement ();

}

Closed root node, only level two

XMLW. WriteEndDocument ();



Closed writer

XMLW. Close ();



Read the contents of the write-in

XmlTextReader reader = new XmlTextReader (Filname);

while (reader. Read ())

{

Gets the node named element

if (reader. LocalName = = ' element ')

{



byte[] bytes = new byte[1000];

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

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



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

}

}

Reader. Close ();



}

}


The above is the easy processing of XML data (4-3) in the. NET framework, and more about topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.