Last post Apr 16, 2012 03:50 AM by kavita_khandhadia
Apr 14, 2012 01:06 AM|tisa|LINK
I have created a program to generate XML string, and i want to set encoding to utf-8, right now it is utf-16. I have try many ways to do it , but still cannot work, anyone can help me?
this is my coding:
Dim sw As New StringWriter()
Dim writer As New XmlTextWriter(sw)
writer.Formatting = Formatting.Indented
writer.Indentation = 3
I cannot put the encoding setting into xmltextwrite class, becuase sw is string, not a file name.
And the ways i tried are XmlWriterSettings,CreateXmlDeclaration and XmlWriter.
Thank you very much!
Apr 14, 2012 02:27 AM|Vipindas|LINK
Hope this helps...
Apr 15, 2012 09:42 PM|decker dong - msft|LINK
You should use MemoryStream to write string things，and then use its WriteTo to write to any other Stream classes……Sample：
Dim ms As New MemoryStream
Dim xmlw As New XmlTextWriter(ms, Encoding.UTF8)
xmlw.WriteElementString("Test", "My Test In Memory……")
ms.WriteTo(New FileStream("c:\\try.xml", FileMode.OpenOrCreate))
Now I've used Reflector (made by Telerik called "Just-Decompiled IT!") to see the original codes：
public XmlTextWriter(TextWriter w) : this()
this.textWriter = w;
this.encoding = w.Encoding;
this.xmlEncoder = new XmlTextEncoder(w);
this.xmlEncoder.QuoteChar = this.quoteChar;
From the codes above you can see that Encoding just depends on TextWriter's，So you cannot directly do settings。
Apr 16, 2012 03:50 AM|kavita_khandhadia|LINK
I have explained this in my blog, check below link.