Solved

XMLWriter, XMLWriterSettings, StrigBuilder and Encoding

Posted on 2006-07-12
4
762 Views
Last Modified: 2012-06-21
Hello

SITUATION:
I'm trying to build a XML document with a XMLWriter.

CODE:
                m_XML = New StringBuilder
            m_Encoding = System.Text.Encoding.GetEncoding(1257)
            m_XMLWriterSettings = New XmlWriterSettings
            With m_XMLWriterSettings
                  .CheckCharacters = True
                  .ConformanceLevel = ConformanceLevel.Document
                  .Encoding = m_Encoding
                  .Indent = True
                  .IndentChars = ControlChars.Tab
                  .NewLineChars = ControlChars.CrLf
                  .NewLineOnAttributes = False
                  .OmitXmlDeclaration = False
            End With

            m_XMLWriter = XmlWriter.Create(m_XML, m_XMLWriterSettings)

PROBLEM:
When I get output from my StringBuilder, I see that encoding in XML declaration is UTF-16 and not windows-1257

QUETION:
How do I get XMLWriter to set encoding in XML declaration to windows-1257

P.S. I can solve this with XMLWriter.WriteRaw() or XMLWriter.WriteProcessingInstruction() methods but I think there is a tiny setting that I am missing to make XMLWriter.WriteStartDocument() method to writer encoding to desired one in XML declaration.

Thanks in advance
Ramuncikas
0
Comment
Question by:Ramuncikas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 17088629
Hello Ramuncikas,

>  m_Encoding = System.Text.Encoding.GetEncoding(1257)
I think thie should be
 m_Encoding = System.Text.Encoding.GetEncoding("windows-1257")



HTH

I
0
 
LVL 14

Author Comment

by:Ramuncikas
ID: 17088639
Did not work as expected. Still utf-16

Thanks though
0
 
LVL 5

Accepted Solution

by:
nickhoggard earned 500 total points
ID: 17088642
Hi,

From what I can understand, .NET stores strings as UTF-16, so when you use XmlWriter.Create with a string builder it has to be in UTF-16.  Try using a memory stream instead of the String builder.

Cheers

Nick

Reference: http://15seconds.com/issue/050616.htm

Quote:
The MemoryStream gives the same output in the page as when using a StringBuilder, but with one important exception. When you send the output to a StringBuilder it is always encoded as Unicode (UTF-16). This is the only character encoding used in the .NET Framework for String values, and so you see the attribute encoding="utf-16" in the opening XML declaration (look back at Figure 6). When using a MemoryStream, however, the stream encoding is set by the XmlWriter and so you see the attribute encoding="utf-8" in the opening XML declaration encoding - which corresponds with the encoding we specified in the XmlWriterSettings instance.

0
 
LVL 14

Author Comment

by:Ramuncikas
ID: 17088923
Thanks, Nick, works like a charm. I actually read this article, but somehow my eyes didn't catch a paragraph you quoted.

Thanks both of you. Good luck

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question