使用.NET根据架构验证XML [英] Using .NET to validate XML against a schema

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

问题描述

我想测试(正确或错误)任意XML文件是否匹配给定架构。



对于它的价值而言,该架构是Word 2003 WordML架构,Microsoft使用大约7个 *。xsd 文件的列表进行定义。



其中一个文件还包括W3C xml.xsd 文件,包括以下语句:

 < ; xsd:import id = xml namespace = http://www.w3.org/XML/1998/namespace 
schemaLocation = http://www.w3.org/2001/xml.xsd >< / xsd:import>

我正在使用.NET代码,如下所示:

  public static void validate(string filename)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(
http://schemas.microsoft.com/office/word/2003/wordml,
//要获取此文件,我下载了 Office 2003: XML参考架构,即 Office2003XMLSchema.exe
@ C:\Program Files\Microsoft Office 2003开发人员资源\Microsoft Office 2003 XML参考架构\WordprocessingML Schema\wordnet.xsd
);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler + =新的ValidationEventHandler(validationEventHandler);
XmlReader xmlReader = XmlReader.Create(文件名,设置);
而(xmlReader.Read()){}
}

我的问题就是如果我在未连接互联网的计算机上运行此代码,则会收到 XmlSchemaValidationException 错误,导致找不到 xml.xsd



要解决此问题,我下载了xml.xsd副本,并使用<$ c明确添加了它。 $ c> settings.Schemas.Add 方法:现在,当计算机未连接到Internet时,验证可以正常工作。



但是当机器已连接到Internet,我现在收到一条错误消息,说全局属性'http://www.w3.org/XML/1998/namespace:lang'已被声明。



显然,我要么需要显式添加,要么不需要,具体取决于计算机是否能够从互联网上静默下载(甚至以前可能已经能够下载它,并将其缓存在某个地方)。



所以,我是该死的,如果我不死的话。我是否需要以一种方式尝试,捕获异常然后以另一种方式尝试?还是有一个更优雅的解决方案?

解决方案

我们看不到您的代码,但是在许多实现方式是通过使用目录解析器将对.xsd的请求重定向到本地副本来解决的。有一个属性 XmlReaderSettings.XmlResolver 可以用于此。有关可以使用的Apache许可的实现,请参见 XMLCatalog.net 。 / p>

此操作的副作用是您可以将所有模式保留在本地缓存。这一点尤其重要,因为W3C会阻止对其网站的过多读取,并且您的代码(或更糟糕的是,您客户的代码)将随机失败。


I want to test (true or false) whether an arbitrary XML file matches a given schema.

For what it's worth, the schema is the Word 2003 WordML schema, which Microsoft defines using a list of about 7 *.xsd files.

One of these files also includes the W3C xml.xsd file, by including the following statement:

<xsd:import id="xml" namespace="http://www.w3.org/XML/1998/namespace"
    schemaLocation="http://www.w3.org/2001/xml.xsd"></xsd:import>

I am using .NET code like the following to do the validation:

   public static void validate(string filename)
    {
       XmlReaderSettings settings = new XmlReaderSettings();
       settings.Schemas.Add(
           "http://schemas.microsoft.com/office/word/2003/wordml",
           //to get this file I downloaded "Office 2003: XML Reference Schemas", i.e. "Office2003XMLSchema.exe" 
           @"C:\Program Files\Microsoft Office 2003 Developer Resources\Microsoft Office 2003 XML Reference Schemas\WordprocessingML Schemas\wordnet.xsd"
           );
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationEventHandler += new ValidationEventHandler(validationEventHandler);
        XmlReader xmlReader = XmlReader.Create(filename, settings);
        while (xmlReader.Read()) { }
   }

My problem is that if I run this code on a machine which is not connected to the internet, then I get a XmlSchemaValidationException error to the effect that it can't find xml.xsd.

To fix this, I downloaded a copy of xml.xsd, and add it explicitly using the settings.Schemas.Add method: the validation now works correctly when the machine is not connected to the internet.

However when the machine is connected to the internet, I now get an error saying that The global attribute 'http://www.w3.org/XML/1998/namespace:lang' has already been declared..

So apparently I either need to add it explicitly, or I don't, depending on whether the machine is able to silently download it from the internet (or even perhaps has previously been able to download it, and has it cached somewhere).

So, it's "damned if I do and damned if I don't". Do I need to try it one way, catch the exception and then try it the other way? Or is there a more elegant solution?

解决方案

We can't see your code, but In many implementations this is handled by redirecting the request for the .xsd to the local copy using a catalog resolver. There is a property XmlReaderSettings.XmlResolver that can be used for this. See XMLCatalog.net for an Apache-licensed implementation you can use.

A side-effect of this is that you can keep all schemas cached locally. This is especially important since W3C will block excessive reads to their site and randomly your code (or worse, your customer's code) will begin to fail.

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

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