在 C# 中编译包含重定义的架构
当读取包含
标签的架构并尝试使用架构集对其进行编译时,出现此异常:
'SchemaLocation' must successfully resolve if <redefine>
contains any child other than <annotation>
我尝试了许多解决方法,但均未成功,例如递归解析架构并将它们添加到架构中在编译之前设置,甚至通过添加它们作为参考来设置。架构仍然无法编译。尝试的示例(解析主 xsd,然后在调用此递归函数后尝试编译生成的“XmlSchemas”):
private void AddRecursive (XmlSchemas xsds, XmlSchema schema)
{
foreach (XmlSchemaExternal inc in schema.Includes) {
String schemaLocation = null;
schemaLocation = inc.SchemaLocation;
XmlSchema xsd;
using (FileStream stream = new FileStream (schemaLocation, FileMode.Open, FileAccess.Read)) {
xsd = XmlSchema.Read (stream, null);
xsds.Add (xsd);
xsds.AddReference (xsd);
}
AddRecursive (xsds, xsd);
}
}
处理此类架构的正确方法是什么?为什么模式编译器无法解析添加的模式本身?
When reading a schema containing <xs:redefine>
tags and trying to compile it using a schema set, I get this exception:
'SchemaLocation' must successfully resolve if <redefine>
contains any child other than <annotation>
I tried many workarounds unsuccessfully like parsing the schemas recursively and adding them to the schema set before compilation and even by adding them as a reference. Still the schema doesn't compile. Example of what was tried (parsing the main xsd and then trying to compile the resulting 'XmlSchemas' after a call to this recursive function):
private void AddRecursive (XmlSchemas xsds, XmlSchema schema)
{
foreach (XmlSchemaExternal inc in schema.Includes) {
String schemaLocation = null;
schemaLocation = inc.SchemaLocation;
XmlSchema xsd;
using (FileStream stream = new FileStream (schemaLocation, FileMode.Open, FileAccess.Read)) {
xsd = XmlSchema.Read (stream, null);
xsds.Add (xsd);
xsds.AddReference (xsd);
}
AddRecursive (xsds, xsd);
}
}
What is the correct way to handle such schemas? Why can't the schema compiler resolve the added schemas itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用基于流的重载读取 XML 架构的问题是,没有可供 XML 架构读取器使用的基本 uri。如果您的 xsd:redefine 在 schemaLocation 属性中使用相对 URI,则默认解析器将无法找到正在重新定义的架构 - 因此您会收到错误消息。
我为您提供以下配置来帮助您继续操作,至少让您了解其工作原理。将两个架构和测试脚本保存在同一文件夹中,更新脚本中的路径并运行 C# 脚本。它会给您以下输出:
如果您更新脚本以使用基于流的重载,您将从帖子中收到错误消息。
基本架构:
重新定义的架构:
测试脚本:
The problem with reading the XML schema using the Stream-based overload is that there is no base uri available to the XML Schema reader. If your xsd:redefine uses a relative URI in the schemaLocation attribute, than the default resolver will not be able to find the schema being redefined - hence the error message you're getting.
I am providing you with the following configuration to get you going, at least in understanding how this is working. Save the two schemas and the test script in the same folder, update the paths in the script and run the C# script. It'll give you this output:
If you update the script to use the Stream-based overload, you'll get the error message from your post.
Base schema:
The schema that redefines:
A test script:
使用 XmlDocument 的相对路径的工作示例:
Working example with relatives paths with XmlDocument: