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#.
views:
2486answers:
4I'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?
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.
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);
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?