【发布时间】:2011-09-01 08:05:06
【问题描述】:
有时,在使用 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”架构但没有成功?
【问题讨论】:
标签: c# xml validation xsd xmlreader