我需要做什么? 我需要针对XSD文件验证 XML文件(传递文件路径/位置)(传递文件路径/位置).我需要检查它是否完好没有非法字符,并且它具有在XSD中定义的所有标签,即没有标签丢失.它匹配
我需要针对XSD文件验证 XML文件(传递文件路径/位置)(传递文件路径/位置).我需要检查它是否完好没有非法字符,并且它具有在XSD中定义的所有标签,即没有标签丢失.它匹配xsd中定义的数据类型.完成之后,我需要解析xml文件以获取数据并将其存储在数据库中.
有问题吗?
1)使用带有XmlDocument的XmlReaderSetttings和带有Validate方法的XmlReader将帮助我实现我需要的东西吗?有没有人用sampel代码帮我?
2)解析xml文件以获取特定标记的最佳方法是什么?
我是VB.net的新手,所以任何示例代码帮助将不胜感激.谢谢!
是的,你走在正确的轨道上.验证XML文档可以使用XmlDocument或XmlReader完成(我稍后将介绍,您也可以使用XDocument).您选择哪一个取决于您的情况,但它们的工作方式类似.当他们发现文档出错时,他们会调用ValidationEventHandler委托. XmlReader通过XmlReaderSettings对象中的事件调用它,而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数据,也可以将数据写入数据库,看看你能得到多少.如果您在遇到麻烦时有任何具体问题,我们将随时为您提供帮助:)
