Question

I have an application which is using to create XML documents on the example of existing. But that's not the point. Today I noticed that there is an error if the opened file encoding is ANSI. Before that I worked with files UTF-8 and this problem does not arise. What should you do and how?

Fragments of code:

string filepath;
XmlDocument xdoc = new XmlDocument();
XmlElement root;
...............
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    filepath = openFileDialog1.FileName;
    textBox1.Text = filepath;
    load();
}
...............
public void load()
{
    xdoc.Load(filepath);
    root = xdoc.DocumentElement;
...............

Error:

An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll Additional information: An invalid character for the specified encoding., Line 35, position 16.

In that line is Cyrillic symbols (russian language). But if I converted this document to UTF-8 by NotePad++ - it loaded correctly.

Was it helpful?

Solution

You could use a StreamReader to read the file with the correct encoding and then load that stream into the XmlDocument overload that accepts a stream.

using(var sr = new StreamReader(filepath, myEncoding))
{
   xdoc.Load(sr);
}

You can obtain myEncoding via the GetEncoding method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top