我有以下xml模式
<?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是根据上述模式进行验证的:
案例1:(模式异常)
<?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:(无例外)
<?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:(无例外)
<RootElement11 name="Configuration">
</RootElement11>对于案例1,我得到了一个预期的异常,即“'http://www.MySchema.net:RootElement1‘元素没有声明”,但是案例2和案例3没有例外地被验证。
我想知道是否有可能在使用XDocument.Validate方法验证带有错误名称空间或没有名称空间的xml文件时抛出异常。
我发现了一些info,它使用带有设置的XmlReader抛出这些类型的异常。我看到了两个可能性: 1)从XmlReader返回到XDocument,2)使用XmlReader验证和使用XDocument执行LINQ查询。但是,如果没有XmlReader,是否有可能完成这一任务。
发布于 2016-05-18 16:11:33
问题是,在每个模式中,案例2和3都是有效的--除了它的targetNamespace之外,您的模式对名称空间中的元素没有任何意见。
XmlReader可以为此返回一个警告,但是XDocument没有过载。您的linked question中的代码片段使用XDocument,我不明白为什么这样做会有任何问题。
https://stackoverflow.com/questions/37304659
复制相似问题