读取Xml文档时,如果XML中有乱码,则会读取文档失败,可以在读取时,将乱码替换掉,方法如下:
///<summary>
根据XML填充ds
</summary>
<paramname="ds">序列化成ds</param>
<paramname="filePath">Xml文档路径publicstaticvoidGetDataFromXML(DataSetds,stringfilePath)
{
try
using(FileStreamfs=new(filePath,FileMode.Open,192)">FileAccess.Read,192)">FileShare.Read))
StreamReadersr=(fs,192)">Encoding.UTF8))
ds.ReadXml(sr);
}
catchXmlException)
//替换乱码
StringReaderstrReader=Regex.Replace(sr.ReadToEnd(),@"[\x00-\x08\x0b-\x0c\x0e-\x1f]",192)">""));
ds.ReadXml(strReader);
strReader.Dispose();
Exceptionex)
log.Error(ex);
兼容有乱码的xml文档
<paramname="tickXmlDoc">读取Xml文档xml文档路径XmlDocumenttickXmlDoc,192)">tickXmlDoc.Load(filePath);
//如果读取失败,则做一次兼容
tickXmlDoc.Load(strReader);
}