使用基于流的重载读取 XML 模式的问题在于 XML 模式读取器没有可用的基本 uri。如果您的 xsd:redefine 在 schemaLocation 属性中使用相对 URI,则默认解析器将无法找到正在重新定义的架构 - 因此您会收到错误消息。
我为您提供了以下配置以帮助您继续前进,至少在了解它是如何工作的方面。将两个架构和测试脚本保存在同一个文件夹中,更新脚本中的路径并运行 C# 脚本。它会给你这个输出:
QN: http://tempuri.org/XMLSchema.xsd:TRedefine, SourceUri: file:///D:/.../.../Redefine.xsd
QN: http://www.w3.org/2001/XMLSchema:anyType, SourceUri:
如果您更新脚本以使用基于流的重载,您将从您的帖子中收到错误消息。
Error line 20: xset.Add(XmlSchema.Read(File.Open (@"D:\...\...\Redefine.xsd", FileMode.Open), null));
'SchemaLocation' must successfully resolve if <redefine> contains any child other than <annotation>.
'SchemaLocation' must successfully resolve if <redefine> contains any child other than <annotation>.
Error Line 21: xset.Add(XmlSchema.Read(File.Open (@"D:\...\...\Redefine.xsd", FileMode.Open), null));
基本架构:
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="TRedefine">
<xsd:sequence>
<xsd:element name="base" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
重新定义的架构:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://tempuri.org/XMLSchema.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- Put a full path here.
<xsd:redefine schemaLocation="D:\...\...\Base.xsd">
-->
<xsd:redefine schemaLocation="Base.xsd">
<xsd:complexType name="TRedefine">
<xsd:complexContent>
<xsd:extension base="TRedefine">
<xsd:sequence/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:redefine>
</xsd:schema>
一个测试脚本:
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
class Script
{
public static void Main()
{
// Enter your code below
// Generated by QTAssistant (http://www.paschidev.com)
XmlSchemaSet xset = new XmlSchemaSet();
// One way of doing using an XmlReader - it'll work with relative URIs.
using(XmlReader reader = XmlReader.Create(@"D:\...\...\Redefine.xsd"))
{
xset.Add(XmlSchema.Read(reader, null));
}
// The other way, using stream, requires all external URIs - xsd:include, xsd:import and xsd:redefine
// to be absolute
//xset.Add(XmlSchema.Read(File.Open(@"D:\...\...\Redefine.xsd", FileMode.Open), null));
xset.Compile();
Console.WriteLine(xset.IsCompiled);
foreach(XmlSchemaType type in xset.GlobalTypes.Values)
{
Console.WriteLine("QN: {0}, SourceUri: {1}", type.QualifiedName, type.SourceUri);
}
}
}