Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 10th, 2006, 08:21 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML and the XMLTextWriter

It seems that most xml is in UTF-8. However, whenever I have a XmlTextWriter create xml in UTF-8 it always puts three additional bytes at the start that screws things up. The resulting xml will not load into a XPathDocument or XmlDocument. If I use ascii encoding it works fine. Can anyone tell me what's happening here?

Code:
            
byte[] b = null;
MemoryStream ms = null;
XmlTextWriter xtw = null;
string ret = "";
Encoding enc = null;

ms = new MemoryStream();
enc = Encoding.UTF8; //Adds extra bytes
//enc = Encoding.ASCII; //Works properly
xtw = new XmlTextWriter(ms, enc);
xtw.WriteStartDocument();
xtw.WriteStartElement("Import");
xtw.WriteStartElement("Indexes");

xtw.WriteStartElement("Index");
xtw.WriteAttributeString("name", "ControlNumber");
xtw.WriteAttributeString("value", "123456789");
xtw.WriteEndElement();

xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Flush();
b = ms.ToArray();

ret = enc.GetString(b);
 
Old May 10th, 2006, 07:42 PM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

those 3 bytes is the Byte Order Mark added because of the Encoding. Don't know total definition for the Byte Order Mark but it's kind of a signature with encoding info and byte order.

You can use another stream with an offset of 3 to remove the extra bytes... or you can change a little your code and even make it more simple... for instance use StringWriter instead of MemoryStream, or create a new class that is derivated from StringWriter where you can pass the Encoding you want, this way:

private class MyStringWriter : StringWriter
{
    Encoding encoding;

    public MyStringWriter(Encoding encoding)
    {
        this.encoding = encoding;
    }

    public override Encoding Encoding
    {
        get {
                     return encoding;
                }
    }
}

your method becomes like this:

  MyStringWriter sw = null;
  XmlTextWriter xtw = null;
  string ret = "";

  sw = new StringWriterWithEncoding(Encoding.UTF8);
  xtw = new XmlTextWriter(sw);
  xtw.WriteStartDocument();
  xtw.WriteStartElement("Import");
  xtw.WriteStartElement("Indexes");

  xtw.WriteStartElement("Index");
  xtw.WriteAttributeString("name", "ControlNumber");
  xtw.WriteAttributeString("value", "123456789");
  xtw.WriteEndElement();

  xtw.WriteEndElement();
  xtw.WriteEndElement();
  xtw.WriteEndDocument();
  xtw.Flush();

  ret = sw.ToString();







Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
Writing XML file Using XmlTextWriter class tmadhavi XML 1 December 19th, 2007 09:20 AM
Serializing object into existing XmlTextWriter planoie .NET Framework 2.0 0 October 15th, 2007 09:54 AM
How to write this by xmlTextWriter yingwen XML 0 April 17th, 2007 09:40 PM
xml invalid top level from ASP write XML(solution) g000we XML 0 August 9th, 2006 03:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.