无法解析中的位置 C# 中的元素
我正在使用 XML 架构文档来验证传入的数据文档,但是该架构在运行时编译期间似乎失败,因为它引用了外部架构的复杂类型。 外部架构在文档顶部的
元素中指定。 我原以为这可能是访问问题,因此我将外部文档的副本移至本地主机文件夹。 我遇到了同样的错误,所以现在我想知道使用
元素是否可能存在某种问题。
架构文档片段如下所示:
<xs:schema targetNamespace="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:cpl="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
...
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd"/>
...
<xs:element name="Signer" type="ds:KeyInfoType" minOccurs="0"/>
...
</xs:schema>
我尝试运行此代码的代码非常简单(从 http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate -xml-against-xsd-xml-schema-using-c.aspx)
string XSDFILEPATH = @"http://localhost/TMSWebServices/XMLSchema/CPL.xsd";
string XMLFILEPATH = @"C:\foo\bar\files\TestCPLs\CPL_930f5e92-be03-440c-a2ff-a13f3f16e1d6.xml";
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.Schemas.Add(null, XSDFILEPATH);
settings.ValidationType = System.Xml.ValidationType.Schema;
System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load(XMLFILEPATH);
System.Xml.XmlReader rdr = System.Xml.XmlReader.Create(new StringReader(document.InnerXml), settings);
while (rdr.Read())
{
}
一切都很顺利,直到在 while 循环之前实例化 XMLReader 对象的行为止。 然后它会失败并出现类型未声明错误。 它尝试查找的类型 KeyInfoType 是在 import 元素的文档之一中定义的。 我已经确保命名空间对齐。 我想知道命名空间定义中的 # 符号是否导致了问题,但删除它们没有任何效果,它只是改变了错误的外观(即“Type 'http://www.w3.org/2000/09/xmldsig:KeyInfoType' 未声明。 “http://www.w3.org/2000/09/xmldsig#:KeyInfoType” rel="nofollow noreferrer">http://www.w3.org/2000/09/xmldsig#:KeyInfoType'未声明。”)
我怀疑我遗漏了
元素的处理过程。 任何建议都非常受欢迎。 谢谢!
I'm using an XML schema document to validate incoming data documents, however the schema appears be failing during compilation at run time because it refers to a complex type which part of an external schema. The external schema is specified in a <xs:import>
element at the top of the document. I had thought it might be an access problem, so I moved a copy of the external document to a localhost folder. I get the same error, so now I'm wondering if there might be some sort of issue with the use of the <xs:import>
element.
The schema document fragment looks like this:
<xs:schema targetNamespace="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:cpl="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
...
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd"/>
...
<xs:element name="Signer" type="ds:KeyInfoType" minOccurs="0"/>
...
</xs:schema>
The code I'm trying to run this with is real simple (got it from http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate-xml-against-xsd-xml-schema-using-c.aspx)
string XSDFILEPATH = @"http://localhost/TMSWebServices/XMLSchema/CPL.xsd";
string XMLFILEPATH = @"C:\foo\bar\files\TestCPLs\CPL_930f5e92-be03-440c-a2ff-a13f3f16e1d6.xml";
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.Schemas.Add(null, XSDFILEPATH);
settings.ValidationType = System.Xml.ValidationType.Schema;
System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load(XMLFILEPATH);
System.Xml.XmlReader rdr = System.Xml.XmlReader.Create(new StringReader(document.InnerXml), settings);
while (rdr.Read())
{
}
Everything goes well until the line that instantiates the XMLReader object just before the while loop. Then it fails with a type not declared error. The type that it's trying to find, KeyInfoType, is defined in one of the the documents in the import element. I've made sure the namespaces line up. I wondered if the # signs in the namespace definitions were causing a problem, but removing them had no effect, it just changed what the error looked like (i.e. "Type 'http://www.w3.org/2000/09/xmldsig:KeyInfoType' is not declared." versus "Type 'http://www.w3.org/2000/09/xmldsig#:KeyInfoType' is not declared.")
My suspicion is that there's something about the processing of the <xs:import>
element that I'm missing. Any suggestions are very welcome. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你只需要添加一行代码即可使其工作:
Marc
I think you need to add just one line of code to make it work:
Marc
好吧,这有点令人困惑。 我尝试了几种不同的方法来做到这一点,包括添加行:
并且我不断收到相同的错误:
该命名空间指定的文档是:
可以从我所在的位置访问该文档,并且我可以找到(看似)有问题的 KeyInfoType在第 152 行键入。
只是为了好玩,我检查了正在验证的文档,发现该类型的架构中定义的元素不在文档中。 模式将其定义为可选(minOccurs =“0”),因此这不是问题。
当该文档导入外部模式文档时,框架编译 schmea 文档的能力几乎就好像有一些奇怪的东西。 有人见过这种行为吗? 尽管谷歌确实给了我很多尝试的建议,但它并没有证明对这个问题卓有成效。 谢谢!
Ok, this is getting a bit baffling. I've tried to do this a few different ways including adding the line:
and I keep getting the same error:
The document specified by that namespace is:
The document is accessible from where I am and I can locate the (seemingly) offending KeyInfoType type at line 152.
Just for fun, I examined the document being validated and found that the element defined in the schema of this type is not located in the document. The schema defines it as optional (minOccurs="0"), so that's not the issue.
It's almost as though there's something weird about the framework's ability to compile a schmea document when that document imports external schema documents. Has anyone seen this behavior? Google has not proven fruitful for this problem, although it did give me a bunch of suggestions to try. Thanks!