简体   繁体   中英

Validate XML using external DTD

I'm trying to validate my XML using external dtd file. Here is XML header:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE CONTEXT SYSTEM "Data.dtd">
<CONTEXT>
...
</CONTEXT>

And here is my code:

        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.DtdProcessing = DtdProcessing.Parse;
        settings.ValidationType = ValidationType.DTD;
        settings.ValidationEventHandler += (sender, args) => Debug.WriteLine(args.Message);
        // Create the XmlReader object.
        XmlReader reader = XmlReader.Create("Data.xml", settings);
        // Parse the file. 
        while (reader.Read());

After running this code I receive in result a lot of errors looks the same way:

The 'CONTEXT' element is not declared.

I've tried to change file name in doctype for obviously nonexistent file, but as result get the same errors. Please tell me where have I been mistaken?

I could reproduce the problem, as a fix I would suggest to set

settings.XmlResolver = new XmlUrlResolver();

that way, the external DTD file is fetched, it seems, otherwise not. The documentation on MSDN says: "Starting with the .NET Framework 4.5.2, this setting has a default value of null.". So it seems, you need to create it explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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