Solved

XMLWriter, XMLWriterSettings, StrigBuilder and Encoding

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Built-In Report Creator / Viewer 5 41
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 77
Why am I losing my session variable value 11 40
CSS styling problem 3 28
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…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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