0% found this document useful (0 votes)
44 views

C# - How To Include Encoding When Creating XML Files - Stack Overflow

The document discusses how to specify the encoding when creating an XML file in C#. It notes that the default encoding when saving without specifying is UTF-8, but that can cause issues with foreign characters. It recommends either using an overload that allows specifying the encoding, or adding an XML declaration and explicitly setting the encoding to UTF-8.

Uploaded by

Paulo Pereira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

C# - How To Include Encoding When Creating XML Files - Stack Overflow

The document discusses how to specify the encoding when creating an XML file in C#. It notes that the default encoding when saving without specifying is UTF-8, but that can cause issues with foreign characters. It recommends either using an overload that allows specifying the encoding, or adding an XML declaration and explicitly setting the encoding to UTF-8.

Uploaded by

Paulo Pereira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

How to include encoding when creating xml files

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.

XmlDocument writer = new XmlDocument();


XmlElement root = writer.CreateElement("PatientFile");
writer.AppendChild(root);

XmlElement ID = writer.CreateElement("ID");
if (!string.IsNullOrEmpty(_a2Data.CurrentRecordId))
{
ID.InnerText = _a2Data.CurrentRecordId;
}
root.AppendChild(ID);

XmlElement patientID = writer.CreateElement("PatientID");


if (!string.IsNullOrEmpty(_a2Data.PatId))
{
patientID.InnerText = _a2Data.PatId;
}
root.AppendChild(patientID);

XmlElement patientName = writer.CreateElement("PatientName");


if (!string.IsNullOrEmpty(_a2Data.PatName))
{
patientName.InnerText = _a2Data.PatName;
}
root.AppendChild(patientName);

XmlElement room = writer.CreateElement("Room");


if (!string.IsNullOrEmpty(_a2Data.RoomName))
{
room.InnerText = _a2Data.RoomName;
}
root.AppendChild(room);
string folderName = ConfigurationManager.AppSettings["PatientXMLFiles"];
if (!Directory.Exists(folderName))
Directory.CreateDirectory(folderName);

string fileName = ConfigurationManager.AppSettings["PatientXMLFiles"] + @"\" +


_a2Data.CurrentRecordId + ".xml";
writer.Save(fileName);

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

1 can you try it ? stackoverflow.com/questions/157646/… – cat916 Aug 31, 2012 at 4:07

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
// ...

XmlDeclaration documentType = writer.CreateXmlDeclaration("1.0", "utf-8", null);


writer.AppendChild(documentType);

XmlElement root = writer.CreateElement("PatientFile");


writer.AppendChild(root);

// ...

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 answered Aug 31, 2012 at 4:27


Dan
9,777 4 47 65

I changed my code to use the following and it works.

-1 XmlDocument writer = new XmlDocument();


XmlDeclaration xmldecl;
xmldecl = writer.CreateXmlDeclaration("1.0", null, null);
xmldecl.Encoding = "UTF-8";
writer.AppendChild(xmldecl);

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

You might also like