3

我有一个在内存中构建的 XmlDocument。我的数据库中存储了一个 XSD 文档。我得到该 XSD 文档的流并尝试验证 XmlDocument。但验证永远不会奏效。即使我在 XmlDocument 中添加无效节点,它也总是会验证。

此外,我在 xml 文档中指定 TargetNameSpace 值,因为它在 XSD 中。但是验证仍然没有发生。

       XmlSchemaSet schemaSet = new XmlSchemaSet();
       schemaSet.Add("http://tempuri.org/test.xsd", XmlReader.Create(stream));
       schemaSet.Compile();

       XmlSchema xmlSchema = null;
       foreach (XmlSchema schema in schemaSet.Schemas())
       {
            xmlSchema = schema;
       }

       //setting targetnamespace as it is there in xsd
       rootElement.SetAttribute("xmlns", xmlSchema.TargetNamespace);

       xmlDoc.Schemas.Add(xmlSchema);
       string error = string.Empty;
       xmlDoc.Validate((sender, e) =>
       {
           switch (e.Severity)
           {
                case XmlSeverityType.Error:
                     error = e.Message;
                break;
                case XmlSeverityType.Warning:
                     LogWarning(e.Message);
                     break;
           }
        });

错误字符串始终为空。我在这里做错了吗?

4

0 回答 0