encoding xml for deserialization in c#

2019-08-28 21:21发布

I have several xml files which I want do deserialize.

var serializer = new XmlSerializer(typeof(Document));
var encoding = Encoding.GetEncoding("Windows-1252");
var sr = new StreamReader(current_file, encoding, true);
var reader = XmlReader.Create(sr);
var i = (Document)serializer.Deserialize(reader);

The problem is that the files have got different encodings. "Windows-1252" and "iso-8859-1". How can I deal with both of them?

2条回答
Evening l夕情丶
2楼-- · 2019-08-28 21:41

Try using a FileStream instead of a StreamReader. The XmlSerializer internally will create an XmlTextReader that will detect the encoding.

var serializer = new XmlSerializer(typeof(Document));

using (var fs = new FileStream(current_file, FileMode.Open))
{
    var i = (Document)serializer.Deserialize(fs);
}

To check which encoding is being used:

Element el1;
Encoding enc1;

using (var fs = new FileStream("Text1252.xml", FileMode.Open))
using (var reader = new XmlTextReader(fs))
{
    reader.MoveToContent();
    enc1 = reader.Encoding;
    el1 = (Element)serializer.Deserialize(reader);
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-28 21:48

I guess this answer obtaining the xml encoding will be helpful to get encoding. When you deserialize it you can use something similar to below;

public static T DeserializeObject<T>(string objectXml, Encoding encoding)
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        MemoryStream memoryStream = new MemoryStream(StringToByteArray(objectXml, encoding));
        T deserializedObject = (T)xs.Deserialize(memoryStream);
        return deserializedObject;
    }
查看更多
登录 后发表回答