Solved

XMLWriter, XMLWriterSettings, StrigBuilder and Encoding

Posted on 2006-07-12
4
738 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now