验证XSD在C#,好像它是一个XML:不给预期的警告 [英] Validate XSD in C# as if it were an XML: doesn't give expected warnings

查看:225
本文介绍了验证XSD在C#,好像它是一个XML:不给预期的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短篇小说:

我想验证XSD文件(所有的XSD是XMLS自理)使用XSD架构,在code,并获得Visual Studio的表现在其(XML作为打开同一XSD文件时同样的警告或错误文本)编辑器。

I want to validate an XSD file (all XSDs being XMLs themselves) using an XSD schema, in code, and get the same warnings or errors that Visual Studio displays when opening the same XSD file in its (xml as text) editor.

的Visual Studio示出了在它的XML编辑警告(见下文)。

Visual Studio shows a warning in its xml editor (see below).

我的问题是,在code完成的验证提供了100%的成功。没有警告,什么都没有。我在想什么?

My problem is, the validation done in code gives 100% success. No warning, nothing. What am I missing?

长的故事:

XSD文件是定义一个XML文件,所以我使用XML验证对W3C XSD架构验证我的自定义XSD。

The XSD file is by definition an XML file, so I'm validating my custom XSD using XML validation against the W3C xsd schema.

这里的XSD进行验证,并警告如用VS 2010:
(点击IMG进行缩放)

Here's the XSD to be validated and a warning as shown by VS 2010:
(click img to zoom)

要饶你了眯眼,这里是一个有点警告的:

To spare you the squinting, here's a bit of the warning:

通配符##的任何允许元素Com.Example.Config:permissionConfig,   并且使得内容模型变得模糊。必须在内容模型   这样形成,使得 [... TL;博士]

Wildcard '##any' allows element 'Com.Example.Config:permissionConfig', and causes the content model to become ambiguous. A content model must be formed such that [... tl;dr]

这是我的自定义XSD:

This is my custom XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema 
    xmlns:tns="Com.Example.Config" 
    elementFormDefault="qualified" 
    targetNamespace="Com.Example.Config" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="Com.Example.Config.PermissionConfigCT" />
  <xs:complexType name="Com.Example.Config.PermissionsCT">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="permissionConfig" type="tns:Com.Example.Config.PermissionConfigCT" />

      <!-- next line triggers the warning -->
      <xs:any minOccurs="0" processContents="lax" />

    </xs:sequence>
  </xs:complexType>
</xs:schema>

检查由VS用于验证的架构(从菜单XML - >模式),显示它的使用:

Checking the schemas used for validation by VS (from menu XML -> Schemas) shows it's using:

  • xsdschema.xsd (FWIW,它是从的的XSD W3C原始模式, SANS 的DTD的部分)
  • xml.xsd (在previous XSD参考,以相对包括
  • xsdschema.xsd (FWIW, it's derived from the W3C original schema for XSD, sans the DTD parts)
  • xml.xsd (referenced in the previous xsd, by a relative include)

这两个文件驻留(我的机器上)位置:

Both files reside (on my machine) here:

C:\ Program Files文件(x86)的\微软的Visual Studio 10.0 \ XML \架构

我在同一个目录下的可执行文件复制他们两个,以便于参考。

I copied them both in the same directory as the executable, for easy referencing.

有关完整,这里是如何的 xsdschema.xsd 定义它的命名空间:

For completeness, here's how the xsdschema.xsd defines its namespaces:

<xs:schema 
  targetNamespace="http://www.w3.org/2001/XMLSchema" 
  blockDefault="#all" 
  elementFormDefault="qualified"  
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xml:lang="EN" version="1.0"
  xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense">

  <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>

  <xs:complexType name="openAttrs">
  [...]

...所以我继续写一些快速确证code,使用VS使用相同的XSD。这是code:

...So I went ahead and wrote some quick validating code, using the same XSDs that VS is using. This is the code:

var configXsdPath = "XMLFile1.xml"; 
  //that's my custom XSD file, don't let the extension fool you

var xmlSchemaForXsdPath = "xsdschema.xsd";

var settings = new XmlReaderSettings
    {
        ValidationType = ValidationType.Schema,
        ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
                        | XmlSchemaValidationFlags.ProcessSchemaLocation
                        | XmlSchemaValidationFlags.ProcessIdentityConstraints
    };


using (var tr = XmlReader.Create(xmlSchemaForXsdPath))
{
    settings.Schemas.Add(XmlSchema.Read(tr, (i_sender, i_args) 
      => {throw new InvalidOperationException(
                 "The validating XSD is itself invalid");} ));
}

settings.ValidationEventHandler += (i_sender, i_args) 
  => { throw new Exception(i_args.Message); };

var reader = XmlReader.Create(configXsdPath, settings);
while (reader.Read())
//parse it all
{ }

......也没有例外弹出任何...

...and no exception pops up whatsoever...

更新的下一步我试着喂彻底无效的XML:我加入此元素的地方自定义的XSD里面:

Update Next I tried feeding outright invalid XML: I added this element somewhere inside the custom XSD:

<aaa!></aaa>

和code给出了预期误差*:

and the code gives the expected error*:

元素'模式'在命名空间 http://www.w3.org/2001/XMLSchema '有无效的子元素AAA。可能元素的列表预计:简单类型,复杂类型,组,attributeGroup,元素,属性,注释,注释在命名空间的 http://www.w3.org/2001/XMLSchema

The element 'schema' in namespace 'http://www.w3.org/2001/XMLSchema' has invalid child element 'aaa'. List of possible elements expected: 'simpleType, complexType, group, attributeGroup, element, attribute, notation, annotation' in namespace 'http://www.w3.org/2001/XMLSchema'.

-

  • 在我简要地说,在某些时候,加入无效的XML不会产生任何错误。这是一个情况下 PEBKAC

推荐答案

简单地说,你想达到什么 - 你的验证程序的跳闸下一行触发警告 - 通过验证您的XSD对 W3C XSD架构是从来没有去上班。这就是所谓的唯一粒子属性,这东西的XSD语言不是能够描述的。问题是,这仅仅是个开始;你最终使用了更多的XSD功能,可靠性较差的提议验证这将是。

Simply put, what you're trying to achieve - your validator to trip on next line triggers the warning - by validating your XSD against the W3C xsd schema is never going to work. That is called Unique Particle Attribution, something that the XSD language is not capable of describing. The thing is, this is just the beginning; the more XSD features you end up using, the less reliable your proposed validation it's going to be.

如果你真的想验证一个XSD,.NET上至少使用的的XmlSchemaSet 。要打开和关闭UPA检查,<一个href="http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemacompilationsettings.enableupacheck.aspx"相对=nofollow>这是你是怎么做的。

If you're really trying to validate an XSD, on .NET at least, use the XmlSchemaSet. To turn on and off the UPA check, this is how you do it.

这篇关于验证XSD在C#,好像它是一个XML:不给预期的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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