问题
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?
回答1:
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;
}
回答2:
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);
}
来源:https://stackoverflow.com/questions/51188323/encoding-xml-for-deserialization-in-c-sharp