?? 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)!