I have a problem where I am working with extended character sets in XML but
I have also found that any time I work with a translation or internally
generated Xml document I get the dreaded message, "Data at the root level is
invalid. Line 1 position 1".
If you run the following code there will be extra bytes at the beginning of
the resulting string. I believe this is some type of BigEndian encoding or
something. My question is this, how do I do this and load the result into
the DOM object and keep all character encoding in tact?
Thanks,
Matt
using System;
using System.Xml;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
MemoryStream ms = new MemoryStream();
// Create Xml
XmlTextWriter writer = new XmlTextWriter(m s, System.Text.Enc oding.UTF8);
writer.WriteSta rtDocument(true );
writer.WriteSta rtElement("data ", "www.contoso.co m");
writer.WriteEnd Element();
writer.WriteEnd Document();
// Flush Document
writer.Close();
// Get resulting document
string text = Encoding.UTF8.G etString(ms.Get Buffer());
Console.WriteLi ne(text);
// Load resulting Xml Document into DOM
XmlDocument xml = new XmlDocument();
try
{
xml.LoadXml(tex t);
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
}
}