有时候,当使用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”模式但没有成功吗?
但是在上面的导入中没有第二个参数。为什么会调用w3网站? - Sai Ram Reddy