Sometimes, when validating certain XML documents using an XmlValidatingReader, I receive the following error:
有时,在使用XmlValidatingReader验证某些XML文档时,我收到以下错误:
System.Xml.Schema.XmlSchemaValidationException:
"The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared."
The same document sometimes succeeds. I cannot figure out why.
同一文件有时会成功。我无法弄清楚为什么。
My XSD imports the schema like so:
我的XSD像这样导入架构:
<xs:schema id="myschemaId"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://mytargetnamespace.com"
xmlns="http://mytargetnamespace.com"
xmlns:mm="http://mytargetnamespace.com"
elementFormDefault="qualified">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2001/xml.xsd" />
...
And in the XML document I have the following attributes:
在XML文档中,我有以下属性:
<root xmlns="http://mytargetnamespace.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mytargetnamespace.com myschema.xsd">
Finally, the XmlReaderSettings:
最后,XmlReaderSettings:
const XmlSchemaValidationFlags validationFlags =
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation |
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.AllowXmlAttributes;
// Set the validation settings.
var settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema,
ValidationFlags = validationFlags,
DtdProcessing = DtdProcessing.Parse
};
settings.ValidationEventHandler += OnValidationEventHandler;
// Create the XmlReader object.
var reader = XmlReader.Create(_xmlFilePath, settings);
// Parse the file.
while (reader.Read()) {}
This is a standalone exe running .NET 4.0 on Windows 2003.
这是在Windows 2003上运行.NET 4.0的独立exe。
I've noticed that there's a significant pause when it's trying to validate. Could that be related? Is it trying to download the actual "xml.xsd" schema and not succeeding?
我注意到它在尝试验证时有一个重要的停顿。这有关系吗?它是否尝试下载实际的“xml.xsd”架构而没有成功?
2 个解决方案
#1
7
Because many of the DTDs and XSDs originated from the W3C, they have the problem that many people try to resolve them from their servers, resulting in their being inundated with requests - millions and millions of them. So they started blocking "excessive" requests.
由于许多DTD和XSD都来自W3C,因此他们遇到了许多人试图从他们的服务器解决这些问题的问题,导致他们被请求淹没 - 数百万和数百万。所以他们开始阻止“过度”请求。
See this blog entry, which also applies to XSDs.
请参阅此博客条目,该条目也适用于XSD。
The solution is to use a local copy.
解决方案是使用本地副本。
#2
3
I'm pretty confident I've solved this one. I checked Fiddler and did see requests going out to w3c.org for the xsd file. A little more research turned up this link; remark #3 seemed to relate to my situation. So if for whatever reason my machine couldn't download the XSD file, then the xml namespace became unavailable. Sadly the real error ("could not reach w3c.org" or what have you) was never reported.
我很自信我已经解决了这个问题。我检查了Fiddler并确实看到了请求w3c.org发送xsd文件的请求。更多的研究发现了这个链接;评论#3似乎与我的情况有关。因此,如果由于某种原因我的机器无法下载XSD文件,则xml命名空间变得不可用。可悲的是,从未报道真正的错误(“无法访问w3c.org”或者你有什么)。
Removing the schemaLocation
from the xs:import
did the trick.
从xs:import中删除schemaLocation就可以了。
#1
7
Because many of the DTDs and XSDs originated from the W3C, they have the problem that many people try to resolve them from their servers, resulting in their being inundated with requests - millions and millions of them. So they started blocking "excessive" requests.
由于许多DTD和XSD都来自W3C,因此他们遇到了许多人试图从他们的服务器解决这些问题的问题,导致他们被请求淹没 - 数百万和数百万。所以他们开始阻止“过度”请求。
See this blog entry, which also applies to XSDs.
请参阅此博客条目,该条目也适用于XSD。
The solution is to use a local copy.
解决方案是使用本地副本。
#2
3
I'm pretty confident I've solved this one. I checked Fiddler and did see requests going out to w3c.org for the xsd file. A little more research turned up this link; remark #3 seemed to relate to my situation. So if for whatever reason my machine couldn't download the XSD file, then the xml namespace became unavailable. Sadly the real error ("could not reach w3c.org" or what have you) was never reported.
我很自信我已经解决了这个问题。我检查了Fiddler并确实看到了请求w3c.org发送xsd文件的请求。更多的研究发现了这个链接;评论#3似乎与我的情况有关。因此,如果由于某种原因我的机器无法下载XSD文件,则xml命名空间变得不可用。可悲的是,从未报道真正的错误(“无法访问w3c.org”或者你有什么)。
Removing the schemaLocation
from the xs:import
did the trick.
从xs:import中删除schemaLocation就可以了。