使用XSD验证XML [英] Validating XML with XSD

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

问题描述

使用 XSD 来验证XML时,我遇到了真正的困难.我应该在所有这些内容前面加上前缀,并预先声明,我是XSD和验证的新手,所以我不确定这是代码问题还是XML问题.我去过XML API的地狱,然后返回了无数种不同的选择,并认为我已经找到了用XSD验证XML的理想策略.请注意,我的XML和XSD来自数据库,因此不需要从磁盘读取任何内容.

I'm running into real difficulties validating XML with XSD. I should prefix all of this and state up front, I'm new to XSD and validation, so I'm not sure if it's a code issue or an XML issue. I've been to XML API hell and back with the bajillion different options and think that I've found what would be the ideal strategy for validating XML with XSD. Note, my XML and XSD are coming from a database, so I don't need to read anything from disk.

我已将问题缩小为一个简单的示例 Windows窗体应用程序.它具有用于XSD的文本框(txtXsd),用于XML的文本框(txtXml),用于结果的文本框(txtResult)和用于启动验证的按钮(btnValidate).

I've narrowed my problem down into a simple sample Windows Forms application. It has a textbox for XSD (txtXsd), a textbox for XML (txtXml), a textbox for the result (txtResult), and a button to start the validation (btnValidate).

我正在使用Microsoft提供的示例XSD文件,

I'm using a sample XSD file from Microsoft,

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:bookstore-schema" elementFormDefault="qualified" targetNamespace="urn:bookstore-schema">
    <xsd:element name="title" type="xsd:string" />
    <xsd:element name="comment" type="xsd:string" />
    <xsd:element name="author" type="authorName"/>
    <xsd:complexType name="authorName">
        <xsd:sequence>
            <xsd:element name="first-name" type="xsd:string" />
            <xsd:element name="last-name" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

我在应用程序中使用以下代码.

I'm using the following code in my application.

private void btnValidate_Click (object sender, EventArgs e)
{
    try
    {
        XmlTextReader reader = new XmlTextReader(txtXsd.Text, XmlNodeType.Document, new XmlParserContext(null, null, String.Empty, XmlSpace.None));
        XmlSchema schema = XmlSchema.Read(reader, null);
        XmlSchemaSet schemas = new XmlSchemaSet();
        schemas.Add(schema);

        XDocument doc = XDocument.Parse(txtXml.Text);
        doc.Validate(schemas, ValidateSchema);
    }
    catch (Exception exception)
    {
        txtResult.Text += exception.Message + Environment.NewLine;
    }
}

private void ValidateSchema (Object sender, ValidationEventArgs e)
{
    txtResult.Text += e.Message + Environment.NewLine;
}

作为测试,我输入了有效的XML,但是我认为不符合上面的XSD.

As a test, I put in valid XML, but what I think should not conform to the XSD above.

<xml>
    <bogusNode>blah</bogusNode>
</xml>

结果一无是处,没有任何验证错误.我该如何解决?

The result is nothing, no validation errors whatsoever. How do I fix it?

推荐答案

好吧,一个-您的XSD定义了XML名称空间xmlns="urn:bookstore-schema",该名称空间在XML测试文件中不存在-因此,XML测试文件中没有任何内容被验证.

Well, for one - your XSD defines a XML namespace xmlns="urn:bookstore-schema" which is not present in your XML test file - therefore, nothing in your XML test file will be validated.

如果您从架构中删除这些元素:

If you remove those elements form your schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="title" type="xsd:string" />

然后它将正确验证您的XML测试文件并抱怨错误的元素.

then it will properly validate your XML test file and complain about the wrong elements.

也不要使用名为<xml>的元素-因为指令<?xml ......?>是预定义的指令,因此不应在文档的其他位置显示为标记名.

Also using an element named <xml> might not be a great idea - since the directive <?xml ......?> is a pre-defined directive and should not appear as tag name elsewhere in your document.

马克

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

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