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

查看:20
本文介绍了使用 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天全站免登陆