I'm trying to open a xml file (ansi) and converting and saving it UTF-8.
Here is my code:
using System;
using System.IO;
using System.Text;
using System.Xml;
class Test
{
public static void Main()
{
string path = @"C:\test\test.xml";
string path_new = @"C:\test\test_new.xml";
try
{
XmlTextReader reader = new XmlTextReader(path);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UTF8Encoding(false);
using (var writer = XmlWriter.Create(path_new, settings))
{
reader.Save(writer);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
I'm getting an error 'System.Xml.XmlTextReader' does not contain a definition for 'Save' and no extension method 'Save' accepting a first argument of type 'System.Xml.XmlTextReader' could be found (are you missing a using directive or an assembly reference?)
What class am I missing here ? Is my code correct to do the job
EDIT:
Okay here another code that is giving me exception:
using System;
using System.IO;
using System.Text;
using System.Xml;
class Test
{
public static void Main()
{
string path = @"C:\project\rdsinfo.xml";
//string path_new = @"C:\project\rdsinfo_new.xml";
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
It's giving me an exception, invalid character in the given encoding.
It is as simple as:
string path = @"C:\test\test.xml";
string path_new = @"C:\test\test_new.xml";
Encoding utf8 = new UTF8Encoding(false);
Encoding ansi = Encoding.GetEncoding(1252);
string xml = File.ReadAllText(path, ansi);
XDocument xmlDoc = XDocument.Parse(xml);
File.WriteAllText(
path_new,
@"<?xml version=""1.0"" encoding=""utf-8""?>" + xmlDoc.ToString(),
utf8
);
Change the ANSI encoding (1252 in the sample) to whichever one your ANSI file is in - see list of encodings.