我在XmlDocument对象中有以下XML,它在未从磁盘加载的内存中创建。
<?xml version="1.0" encoding="UTF-8"?>
<Request Attribute1="10" Attribute2="20" Attribute3="Someone">
<Parameters />
</Request>
并使用以下xsd验证上面的XML,它是从磁盘加载的
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Request">
<xs:complexType>
<xs:sequence>
<xs:element name="Parameters" />
</xs:sequence>
<xs:attribute name="Attribute1" type="xs:unsignedByte" use="required" />
<xs:attribute name="Attribute2" type="xs:unsignedByte" use="required" />
<xs:attribute name="Attribute3" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
并在下面找到用于验证的c#代码,我使用的是.net framework 4.0
bool bCorrect = false;
XmlSchemaSet xmlSchemas = new XmlSchemaSet();
xmlSchemas.Add(nsAVFSale, xsdAVFSale);
xmlSchemas.Compile();
foreach (XmlSchema schema in xmlSchemas.Schemas())
nsAVFSale = schema.TargetNamespace;
xml.DocumentElement.SetAttribute("xmlns:xs", nsAVFSale);
if (xmlSchemas.IsCompiled && xmlSchemas.GlobalElements.Contains(new XmlQualifiedName(xml.DocumentElement.LocalName, this.nsAVFSale)))
{
xml.Schemas = xmlSchemas;
xml.Validate(new ValidationEventHandler((sender, e) =>
{
if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
throw e.Exception;
else
bCorrect = true;
}));
}
我永远不会得到bCorrect = true或得到e.Exception,请你告诉我这里做错了什么。