はい、あなたは正しい方向にあります。 XmlDocument
またはXmlReader
のいずれかを使用してXML文書の検証を行うことができます(後で説明しますが、XDocument
も使用できます)。どちらを選択するかは状況によって異なりますが、どちらも同じように機能します。ドキュメントでエラーが見つかると、ValidationEventHandler
代理人を呼び出します。 XmlReader
はオブジェクトのイベントを介して呼び出されますが、XmlDocument
はValidate
メソッドのパラメータとして渡されたデリゲートを介して呼び出されます。ここでは、エラーを収集するために使用することができる単純なクラスです。このクラスで
Public Class XmlValidationErrorBuilder
Private _errors As New List(Of ValidationEventArgs)()
Public Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Error Then
_errors.Add(args)
End If
End Sub
Public Function GetErrors() As String
If _errors.Count <> 0 Then
Dim builder As New StringBuilder()
builder.Append("The following ")
builder.Append(_errors.Count.ToString())
builder.AppendLine(" error(s) were found while validating the XML document against the XSD:")
For Each i As ValidationEventArgs In _errors
builder.Append("* ")
builder.AppendLine(i.Message)
Next
Return builder.ToString()
Else
Return Nothing
End If
End Sub
End Class
ValidationEventHandler
方法はValidationEventHandler
デリゲートのシグネチャと一致するので、あなたはXmlReader
かのいずれかからエラーを収集するためにそれを使用することができますXmlDocument
。ここでは、あなたがXmlDocument
でそれを使用することができます方法は次のとおりです。
Public Function LoadValidatedXmlDocument(xmlFilePath As String, xsdFilePath As String) As XmlDocument
Dim doc As New XmlDocument()
doc.Load(xmlFilePath)
doc.Schemas.Add(Nothing, xsdFilePath)
Dim errorBuilder As New XmlValidationErrorBuilder()
doc.Validate(New ValidationEventHandler(AddressOf errorBuilder.ValidationEventHandler))
Dim errorsText As String = errorBuilder.GetErrors()
If errorsText IsNot Nothing Then
Throw New Exception(errorsText)
End If
Return doc
End Function
そして、ここでは、あなたがXmlReader
でそれを使用することができます方法は次のとおりです。また
Public Sub LoadXml(xmlFilePath As String, xsdFilePath As String)
Dim settings As New XmlReaderSettings()
settings.Schemas.Add(Nothing, xsdFilePath)
settings.ValidationType = ValidationType.Schema
Dim errorBuilder As New XmlValidationErrorBuilder()
AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf errorBuilder.ValidationEventHandler)
Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings)
' Read the document...
Dim errorsText As String = errorBuilder.GetErrors()
If errorsText IsNot Nothing Then
' Handle the errors
End If
End Function
、あなたはまた、新しいXDocument
クラスを使用することができます。 XDocument
でそれを行う方法はXmlDocument
と非常に似ています。 XDocument
のためのValidate
拡張メソッドがありますが、これもまたValidationEventHandler
デリゲートをとります。ここではその一例です:
Public Function LoadValidatedXDocument(xmlFilePath As String, xsdFilePath As String) As XDocument
Dim doc As XDocument = XDocument.Load(xmlFilePath)
Dim schemas As New XmlSchemaSet()
schemas.Add(Nothing, xsdFilePath)
Dim errorBuilder As New XmlValidationErrorBuilder()
doc.Validate(schemas, New ValidationEventHandler(AddressOf errorBuilder.ValidationEventHandler))
Dim errorsText As String = errorBuilder.GetErrors()
If errorsText IsNot Nothing Then
Throw New Exception(errorsText)
End If
Return doc
End Function
データベースにXML文書からデータをロードするため、それは正確には、XML文書のスキーマを知らなくても、それを行う方法を、言うことは不可能だとして、のスキーマは、データベース、データベースの種類などです。私は、XMLデータの読み込みとデータベースへのデータの書き込みの両方を研究することをお勧めします。問題が発生したときに具体的な質問がある場合は、私たちが手伝ってくれます:)
[VB.netのスキーマに対してXMLを検証する方法](http://stackoverflow.com/questions/) 15088585/how-to-validate-xml-against-vb-net) –