简体   繁体   English

给定编码中的无效字符

[英]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;
    }
}

I got xml file from third party which also include the Document Type Definition file after xml declaration. 我从第三方获得了xml文件,该文件还包含xml声明后的文档类型定义文件。

<?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>

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 第102行,位置56。如果我捕获到异常并重试解析文件,那么我又遇到了另一个问题,即文件解析,但是

I got the error Could not find file 'C:\\Windows\\system32\\SoccerMatchPlus.dtd'. 我收到错误“找不到文件C:\\ Windows \\ system32 \\ SoccerMatchPlus.dtd”。

Document Type Definition file named SoccerMatchPlus.dtd is added before the root element by third party. 第三方在根元素之前添加了名为SoccerMatchPlus.dtd的文档类型定义文件。

In the case of Load method the parser loads the file from the location where xml file also exists. 对于Load方法,解析器从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? 我将SoccerMatchPlus.dtd放置在xml文件所在的其他位置,可以在运行时从指定位置加载SoccerMatchPlus.dtd文件,还是可以告诉我加载包含无效字符数据的xml文件的更好方法?

Use the XmlResolver property of XmlDocument class to disable DTD processing. 使用XmlDocument类的XmlResolver属性禁用DTD处理。

XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.XmlResolver = null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM