Invalid character in the given encoding

2019-02-19 07:39发布

问题:

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

I got the error Could not find file 'C:\Windows\system32\SoccerMatchPlus.dtd'.

Document Type Definition file named SoccerMatchPlus.dtd is added before the root element by third party.

In the case of Load method the parser loads the file from the location where xml file also exists.

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?

回答1:

Use the XmlResolver property of XmlDocument class to disable DTD processing.

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