正在验证无效的 XDocument [英] Invalid XDocument is getting validated

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

问题描述

我有以下 xml 架构

I have following xml schema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" 
           targetNamespace="http://www.MySchema.net" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="RootElement">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="name" type="xs:string" />
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

和以下 xml 是根据上述架构验证的:

and following xml's are validated against above schema:

案例 1:(架构异常)

Case 1: (Schema exception)

<?xml version="1.0" encoding="utf-8" ?> <RootElement11 name="Configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.MySchema.net Root" xmlns="http://www.MySchema.net">
    </RootElement11>

情况 2:(也不例外)

Case 2: (No exception)

<?xml version="1.0" encoding="utf-8" ?>
<RootElement11 name="Configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.YourSchema.net Root" xmlns="http://www.YourSchema.net">
</RootElement11>

情况 3:(也不例外)

Case 3: (No exception)

<RootElement11 name="Configuration">
</RootElement11>

对于案例 1,我得到一个预期的异常'http://www.MySchema.net:RootElement1' 元素未声明.",但案例 2 和案例 3 无一例外地得到验证.

For Case 1, I get an expected exception that "The 'http://www.MySchema.net:RootElement1' element is not declared.", but Case 2 and Case 3 are validated without exception.

我想知道当使用 XDocument.Validate 方法验证具有错误命名空间或没有命名空间的 xml 文件时是否有可能引发异常.

I wanted to know if there is a possibility to throw an exception when xml files with false namespaces or without namespaces are validated using XDocument.Validate method.

我发现了一些信息,它们使用带有设置的 XmlReader 来抛出这些类型的异常.我看到两种可能性 1) 从 XDocument 返回 XmlReader,2) 使用 XmlReader 进行验证并使用 XDocument 进行 LINQ 查询.但是没有 XmlReader 是否可以实现这一点.

I found some info which use XmlReader with settings to throw these type of exception. I see two possibilites 1) Get back to XmlReader from XDocument, 2) Validate using XmlReader and use XDocument to do LINQ queries. But is it possible to accomplish this without XmlReader.

推荐答案

问题是情况 2 和 3 对每个架构都是有效的 - 您的架构对命名空间中的元素没有任何意见而不是它的 目标命名空间.

The issue is that both cases 2 and 3 are valid per the schema - your schema doesn't have any opinion on elements in namespaces other than its targetNamespace.

XmlReader 可以为此返回警告,但是 XDocument 没有重载可以做到这一点.链接问题中的代码段使用了XmlReader XDocument,我不明白为什么你这样做会有任何问题.

XmlReader can return a warning for this, but there's no overload for XDocument that will do it. The snippet in your linked question uses an XmlReader wrapper around XDocument, I can't see why you'd have any issue with doing it this way.

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

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