Validating xml against the Onix 2.1 dtd with dotNe

2019-08-13 18:08发布

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.

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?

Here's my code...

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);
        });

1条回答
贼婆χ
2楼-- · 2019-08-13 18:36

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

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

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

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)) { ... }
查看更多
登录 后发表回答