Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XMLWriter, XMLWriterSettings, StrigBuilder and Encoding

Posted on 2006-07-12
4
Medium Priority
?
768 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 2000 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

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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