给定编码中的无效字符 [英] Invalid character in the given encoding
问题描述
XmlDocument oXmlDoc = new XmlDocument();
try
{
oXmlDoc.Load(filePath);
}
catch (Exception ex)
{
// Log Error Here
try
{
Encoding enc = Encoding.GetEncoding("iso-8859-1");
StreamReader sr = new StreamReader(filePath, enc);
String response = sr.ReadToEnd();
oXmlDoc.LoadXml(response);
}
catch (Exception innerException)
{
// Log Error Here
return false;
}
}
我从第三方那里获得了xml文件,该文件还包含xml声明后的文档类型定义文件.
I got xml file from third party which also include the Document Type Definition file after xml declaration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SoccerMatchPlus SYSTEM "SoccerMatchPlus.dtd">
<SoccerMatchPlus matchid="33226">
<Booking id="13642055" time="47">
<Player id="370927">
<Name firstName="Lasse" initials="L" lastName="Nielsen">L Nielsen</Name>
</Player>
<Team id="26415" name="AæB" homeOrAway="Home"/>
</Booking>
</SoccerMatchPlus>
如果我使用给定的编码来解析带有无效字符的文件.第102行,位置56.如果我捕获到异常并重试解析文件,那么我又遇到了另一个问题,即文件解析,但是
If I parse the file with Invalid character in the given encoding. Line 102, position 56. If I catch the exception and retry to parse the file then I got another issue, file parses but
我收到错误消息找不到文件C:\ Windows \ system32 \ SoccerMatchPlus.dtd".
I got the error Could not find file 'C:\Windows\system32\SoccerMatchPlus.dtd'.
由第三方在根元素之前添加名为SoccerMatchPlus.dtd的文档类型定义文件.
Document Type Definition file named SoccerMatchPlus.dtd is added before the root element by third party.
对于Load方法,解析器从xml文件也存在的位置加载文件.
In the case of Load method the parser loads the file from the location where xml file also exists.
我将SoccerMatchPlus.dtd放在xml文件所在的其他位置,我可以在运行时从指定位置加载SoccerMatchPlus.dtd文件,还是可以告诉我一种更好的方式来加载包含无效字符数据的xml文件?
I put the SoccerMatchPlus.dtd in other location where xml file resides, can I load that SoccerMatchPlus.dtd file from the specified location at runtime or can you tell me the better way to load the xml file which contains the invalid characters data?
推荐答案
使用 XmlDocument
类的 XmlResolver
属性禁用DTD处理.
Use the XmlResolver
property of XmlDocument
class to disable DTD processing.
XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.XmlResolver = null;
这篇关于给定编码中的无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!