??The code in Figure 8 demonstrates converting a string data into a Base64 encoded XML stream. Figure 9 is the output result.
Figure 8 Persisting a String Array as Base64
using System;
using System.Text;
using System.IO;
using System.
public static void Main(String[] args)
{
string outputFileName = 'test64.xml';
if (args.Length > 0)
outputFileName = args[0]; // file name
//Convert the 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);
/ / Makes child elements indented based on Indentation and IndentChar settings. This option only indents the element content
xmlw.Formatting = Formatting.Indented;
//Write the XML declaration with version "1.0"
xmlw.WriteStartDocument();
//Write the XML declaration containing the specified text Comments .
xmlw.WriteComment('Array to Base64 XML');
//Start writing out the array node
xmlw.WriteStartElement('array');
//Write out the specified prefix, local name, namespace URI and value attributes
xmlw.WriteAttributeString('xmlns', 'x', null, 'dinoe:msdn-mag');
// Write the child nodes of the array in a loop
foreach(string s in theArray )
{
//Write the specified opening tag and associate it with the given namespace and prefix
xmlw.WriteStartElement('x', 'element', null);
//Put S Convert it into a byte[] array, and encode the byte[] array into Base64 and write the result text. The number of bytes to be written is twice the total length of s. The number of bytes occupied by a string is 2 bytes.
xmlw.WriteBase64(Encoding.Unicode.GetBytes(s), 0, s.Length*2);
//Closed child node
xmlw.WriteEndElement();
}
//Closed root node, There are only two levels
xmlw.WriteEndDocument();
// Close writer
xmlw.Close();
// Read out the written content
XmlTextReader reader = new XmlTextReader(filname);
while(reader.Read())
{
//Get 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(); For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!