Solved

XMLWriter, XMLWriterSettings, StrigBuilder and Encoding

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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