我正在使用XmlDocument.Load来加载具有泰语字符的XML文件的内容。应用程序出错,但有以下异常。
System.Xml.XmlException:给定编码中的字符无效。线 2,位置82.在System.Xml.XmlTextReaderImpl.Throw(例外e) 在System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, INT32和放大器; charsCount)在System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount)在System.Xml.XmlTextReaderImpl.ReadData()at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos,Int32& endPos, INT32和放大器; outOrChars) System.Xml.XmlTextReaderImpl.FinishPartialValue()at System.Xml.XmlTextReaderImpl.get_Value()at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)at at System.Xml.XmlDocument.Load(XmlReader reader)
注意结束标记之前的奇怪字符。此内容来自第三方,我无权访问文件/内容。
我的问题是:
答案 0 :(得分:0)
第三方提供的数据不是有效的XML。我认为只有两个解决方案,即让第三方提供有效的XML或从XML中删除无效字符并处理您可以做的事情。你可以这样做......
string invalidXML = File.ReadAllText(path);
var validXml = invalidXML.Where(ch => XmlConvert.IsXmlChar(ch)).ToArray()
if (validXml != invalidXML)
// log the invalid
// process (what you can in) the validXml
答案 1 :(得分:0)
如果您确定它们是泰语字符,那么请在加载中尝试正确的数据编码。
对于泰语,字符编码为 - ISO 8859-11
那么请你尝试下面的doc加载方式:
xmlDoc.Load(new StreamReader(File.Open("YourXMLFile.xml"),
Encoding.GetEncoding("iso-8859-11")));
回答第一个问题,您可能需要与第三方交谈并要求他们查看他们的源代码,以找出生成的XML中出现这些不需要的字符的原因。