UTF8 Encoding

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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);
}
}
}


.



Relevant Pages

  • RE: XML documentation file name
    ... MSDN document on VB.NET Project Designer says "The Generate XML document ... XML documentation is automatically emitted into an XML ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Dynamic XML document processing.
    ... say you have an application which processes some XML document, ... perhaps creating SVG or HTML output to be displayed in the browser. ... There are a number of XML parsers for js out there and I imagine it ... of extended or additional interfaces grouped in modules such as HTML, ...
    (comp.lang.javascript)
  • RE: error sending � as xml to a web service
    ... <!DOCTYPE sample [ ... But I get a validation error: reference to non-SGML character. ... This is why you can't use it in an XML document, ... if it WERE legal to have a null character in an XML document (it is ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: XMLTextReader reading too many characters
    ... It was a very simple process to delete the extra tag. ... You do know about XML ... status isn't indicative of being capable of editing an XML document. ... If you claim there is a problem with XmlTextReader and an allegedly ...
    (microsoft.public.dotnet.xml)
  • Re: Create XML tree from another partial tree
    ... When I have a part of an xml document, all elements ... Those extra parameters in the SGML element type declarations control the allowability or otherwise of missing start-tags and end-tags. ... You can prove it by running it through onsgmlnorm, provided your CATALOG file supplies a copy of the original v1.28 HTML DTD and a copy of the SGML Declaration for HTML which came with it: ...
    (comp.text.xml)