load foreign characters from xml file using StreamReader

Am loading an XML file but at the same time there are some Greek characters inside the file like "ναι" and when I load them on a data grid view table they appear like ���.

Am using xmlreader to load it with the encoding iso-8859-7 like

public XmlDocument LoadDocument(String x)
{
    XmlDocument document = new XmlDocument();
    using (StreamReader stream = new StreamReader(x, Encoding.GetEncoding("iso-8859-7")))
    {
        document.Load(stream);

    }
    return (document);
}
Jon Skeet
people
quotationmark

The simplest answer here is not to use StreamReader at all. Let the XML parser handle the encoding appropriately:

public XmlDocument LoadDocument(String x)
{
    XmlDocument document = new XmlDocument();
    using (var stream = File.OpenRead(x))
    {
        document.Load(stream);

    }
    return document;
}

Or just use XmlDocument.Load(string):

public XmlDocument LoadDocument(String x)
{
    XmlDocument document = new XmlDocument();
    document.Load(x);
    return document;
}

The XML document should specify its encoding in the XML declaration if it needs to - and that's the best source of truth.

people

See more on this question at Stackoverflow