UTF8 Encoding
- From: "Matt" <mdframe@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 15 Aug 2006 16:12:38 -0400
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(ms, System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.WriteStartElement("data", "www.contoso.com");
writer.WriteEndElement();
writer.WriteEndDocument();
// Flush Document
writer.Close();
// Get resulting document
string text = Encoding.UTF8.GetString(ms.GetBuffer());
Console.WriteLine(text);
// Load resulting Xml Document into DOM
XmlDocument xml = new XmlDocument();
try
{
xml.LoadXml(text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
.
- Follow-Ups:
- Re: UTF8 Encoding
- From: Martin Honnen
- Re: UTF8 Encoding
- Prev by Date: XML und Parameter load
- Next by Date: RE: Detailed ASP.Net info not displaying in browser
- Previous by thread: XML und Parameter load
- Next by thread: Re: UTF8 Encoding
- Index(es):
Relevant Pages
|