简体   繁体   English

使用外部DTD验证XML

[英]Validate XML using external DTD

I'm trying to validate my XML using external dtd file. 我正在尝试使用外部dtd文件验证我的XML。 Here is XML header: 这是XML标头:

<?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. 我试图在doctype中更改明显不存在的文件的文件名,但结果会得到相同的错误。 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. 这样,外部DTD文件被提取,看起来,否则不是。 The documentation on MSDN says: "Starting with the .NET Framework 4.5.2, this setting has a default value of null.". MSDN上的文档说:“从.NET Framework 4.5.2开始,此设置的默认值为null。”。 So it seems, you need to create it explicitly. 所以看来,你需要明确地创建它。

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

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