有时,在使用 XmlValidatingReader 验证某些 XML 文档时,我收到以下错误:
System.Xml.Schema.XmlSchemaValidationException:
"The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared."
同一个文档有时会成功。我不明白为什么。
我的 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" />
...
在 XML 文档中我有以下属性:
<root xmlns="http://mytargetnamespace.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mytargetnamespace.com myschema.xsd">
最后,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()) {}
这是在 Windows 2003 上运行 .NET 4.0 的独立 exe。
我注意到在尝试验证时有一个明显的停顿。会不会有关系?是否尝试下载实际的“xml.xsd”模式但没有成功?
最佳答案
因为许多 DTD 和 XSD 源自 W3C,所以它们存在许多人试图从他们的服务器解析它们的问题,导致它们被请求淹没 - 数以百万计的请求。所以他们开始阻止“过多”的请求。
参见 this blog entry ,这也适用于 XSD。
解决方案是使用本地副本。
关于c# - "The ' http ://www. w3.org/XML/1998/namespace:lang ' attribute is not declared.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6117627/