使用dotNet针对Onix 2.1 dtd验证xml [英] Validating xml against the Onix 2.1 dtd with dotNet

查看:85
本文介绍了使用dotNet针对Onix 2.1 dtd验证xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据ONIX 2.1 dtd验证XML Feed。当我将生成的XML文件加载到XMLSpy中并针对DTD进行验证时,它告诉我Feed是有效的。

I'm trying to validate an XML feed against the ONIX 2.1 dtd. When I load the generated XML file into XMLSpy and validate against the DTD, it tells me that the feed is valid.

当我尝试使用C#和XmlReader,我收到以下错误消息:尽管先前已通过第三方工具验证,但子节点无效。为了确保使用XmlReader的代码正确读取dtd并进行适当的验证,我该怎么做?

When I try to validate the same file using C# and XmlReader, I receive errors that child nodes are invalid despite being validated earlier by the 3rd party tool. What do I need to do in order to ensure that my code using XmlReader is correctly reading the dtd and validating appropriately?

这是我的代码...

XmlReaderSettings settings = new XmlReaderSettings();
        settings.ProhibitDtd = false;
        settings.ValidationType = ValidationType.DTD;
        settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler += new ValidationEventHandler(delegate(object sender, ValidationEventArgs args)
        {
            isXmlValid = false;
            xmlValMsg.AppendLine(args.Message);
        });


推荐答案

问题可能出在DTD上。您可以尝试使用在线DTD和模式验证器...

The problem could be with the DTD. There is an online DTD and Schema validator that you could try...

http://www.validome.org/grammar/

您可以尝试针对XSD进行验证。您可以在 http://www.editeur上找到Onix 2.1 xsd。 org / 15 / Previous-Releases /#R%202.1%20Downloads 。您将必须设置默认名称空间:

You could try to validate against the XSD instead. The Onix 2.1 xsd is available at http://www.editeur.org/15/Previous-Releases/#R%202.1%20Downloads. You will have to set the default namespace:

var nt = new NameTable();
var ns = new XmlNamespaceManager(nt);
ns.AddNamespace(string.Empty, "http://www.editeur.org/onix/2.1/reference");
var context = new XmlParserContext(null, ns, null, XmlSpace.None);

加载xml时,关闭DTD解析:

When loading the xml, turn off DTD parsing:

var settings = new XmlReaderSettings
    {
        ValidationType = System.Xml.ValidationType.Schema,
        DtdProcessing = DtdProcessing.Ignore
    };
using(var reader = XmlReader.Create("path to xml file", settings, context)) { ... }

这篇关于使用dotNet针对Onix 2.1 dtd验证xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆