我正在尝试使用VB.net针对架构验证xml文件。我的问题是,与模式完全匹配的文件仍然没有错误传递。这是我正在使用的代码:
Private Sub ValidateDelete(oStream As Stream, sExt As String)
_DeleteErrorMessage = ""
Dim XmlFileDoc As New XmlDocument
Dim oSettings As XmlReaderSettings = New XmlReaderSettings()
Dim scSet As New XmlSchemaSet
Dim sSchemapath As String = AppSettings("DeleteSchemaFile")
Using scRead As XmlReader = XmlReader.Create(New FileStream(sSchemapath, FileMode.Open, FileAccess.Read), Nothing, sSchemapath)
scSet.Add(XmlSchema.Read(scRead, Nothing))
End Using
scSet.CompilationSettings = New XmlSchemaCompilationSettings
scSet.Compile()
oSettings.ValidationType = ValidationType.Schema
oSettings.Schemas = scSet
AddHandler oSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf ValidationEventHandler)
Dim oreader As XmlReader = XmlReader.Create(oStream, oSettings)
Try
XmlFileDoc.Load(oreader)
Catch xex As XmlException
_DeleteErrorMessage = xex.Message
ValidXML = False
Catch ex As Exception
Throw ex
Finally
oreader.Close()
End Try
End Sub
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
If e.Severity = XmlSeverityType.Error Then
_DeleteErrorMessage = e.Message
End If
End Sub
这是我正在验证的xsd文件。如您所见,它非常简单。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="patientlist">
<xs:complexType>
<xs:sequence>
<xs:element name="patient" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="hcoid" type="xs:unsignedInt" />
<xs:element name="vendor-tracking-id">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"></xs:minLength>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="report-year" >
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="4"></xs:length>
<xs:pattern value="[0-9][0-9][0-9][0-9]"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="report-quarter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="1"></xs:length>
<xs:pattern value="[1-4]"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
该文件允许客户从我们的系统中删除记录。如果我验证缺少元素或具有无效值的删除文件,它工作正常,我得到预期的错误消息。如果我验证一个不是删除文件的文件,我没有收到任何错误消息。它没有问题。
谁能告诉我这里我做错了什么?