tags:

views:

2486

answers:

4

i have a long xml document just created by string builder with the starting tag like <?xml version="1.0" encoding="UTF-8"?> <xxxxxx> .. </xxxxxx> and i want to convert this xml to iso-8859-9 encoding type. How can i do this? Or anyone suggests me another way to create ISO-8859-9 encoding type xml in C#.

+3  A: 

I'd suggest that the most robust way would be to load it as an XML document, and then save it with a TextWriter which has an encoding of ISO-8859-9. That way you don't need to worry about anything XML-specific.

How do you want the output? In a string, a file, a byte array?

Jon Skeet
A: 

i just want a xml file output. So, according to your suggestions what i have to do is: i've already a string. Assume that my string name is:xmldocstring ` XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(xmldocstring);

` and then how can i save it as a xml file with TextWriter in ISO-8859-9 encoding.

enormous81
@enormous81: I see that you're new here, but in the future you should respond to answers by commenting on the answer itself or by editing your question to clarify it.
Mr. Shiny and New
+3  A: 

Since encoding only makes sense when text is encoded into a stream, I assume you want to save the document to a file using the given encoding. That way, the encoding attribute will match the file's encoding.

Try:

using System.IO;
using System.Xml;

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Stream stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.GetEncoding("ISO-8859-9");
XmlWriter writer = XmlWriter.Create(stream, settings);
doc.Save(writer);
Tor Haugen
A: 

thank you for your answer it is really helpfull for me. Besides that i noticed that the sample code below.

string xml ="our created xml string";

                    HttpResponse Response = context.Response;
                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = "application/xls";
                    Response.Charset = "UTF-8";
                    Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
                    Response.AddHeader("content-disposition", "attachment; filename=text.xml" ) ;

Response.Output.Write(xml);

if i just change the Charset property of Response and ContentEncoding Property of Response, Can i reach the your solution? İ will only change these two lines:

Response.Charset = "ISO-8859-9";
Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-9");

does it works?

enormous81
That would probably work.
Tor Haugen