How Do I...Write Binary Data?
This sample illustrates how to write binary data in an XML stream. The WriteBase64 method
encodes the specified binary bytes as Base64 and writes out the resulting text.
After the binary data is written out, it is read into an XmlReader to ensure that the data is valid.
C# BinaryDataInXml.exe
[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Xml\BinaryDataInXml\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
The following code writes the Base64 encoded data inside the element.
using (StringWriter sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw))
{
writer.WriteStartElement("Test");
writer.WriteAttributeString("Length", buffer.Length.ToString());
writer.WriteCData(Convert.ToBase64String(buffer, 0, buffer.Length));
writer.WriteEndElement();
}
...
}
C#
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|