C# - How To Include Encoding When Creating XML Files - Stack Overflow
C# - How To Include Encoding When Creating XML Files - Stack Overflow
Asked 11 years, 7 months ago Modified 5 years, 3 months ago Viewed 6k times
I want to create an XML file to store information. I am using the following code. I would like to know how
to specify the encoding for this file in code.
0
When I try to read this file in another form the characters in Japanese are distorted.
XmlElement ID = writer.CreateElement("ID");
if (!string.IsNullOrEmpty(_a2Data.CurrentRecordId))
{
ID.InnerText = _a2Data.CurrentRecordId;
}
root.AppendChild(ID);
c# xml
Share Edit Follow edited Jan 12, 2019 at 19:55 asked Aug 31, 2012 at 4:03
ivcubr Pradeep Singh
2,038 9 20 29 23 2 6
Please show your XML reading code as it is likley where the problem is. There is nothing particularly wrong with
code you shown (also you can safely cut all of the sample to 3-4 lines). – Alexei Levenkov Aug 31, 2012 at 4:25
2 Answers Sorted by: Highest score (default)
You are using the overload of the Save method that takes a file name as a parameter. This method uses
the UTF-8 encoding. Create an xml declaration before you create the root:
3
// ...
// ...
As a side note, if you want to have control over the encoding that the file is created with, you need to use
one of the other overloads of the Save method.
Share Edit Follow edited Jan 12, 2019 at 20:58 answered Aug 31, 2012 at 4:38
ivcubr Pradeep Singh
2,038 9 20 29 23 2 6