首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

xml文件的有效性验证,该如何处理

2012-01-08 
xml文件的有效性验证我把导入到datagrid的xml文件进行一次有效性验证,代码如下:Public Class Form1Public

xml文件的有效性验证
我把导入到datagrid的xml文件进行一次有效性验证,代码如下:
Public Class Form1
  Public ReadOnly Property schemafilename() As String
  Get
  Dim fileinfo As New IO.FileInfo(Application.ExecutablePath)
  Dim foldername As String = fileinfo.DirectoryName
  Return foldername & "\xbrlwendang.xml"
  End Get
  End Property
  Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
  dlgopenfile.Filter = "XML Files(*.xml)|*.xml|ALL Files(*.*)|*.*||"
  If dlgopenfile.ShowDialog = Windows.Forms.DialogResult.OK Then
  Try
  Dim newdataset As New DataSet
  newdataset.ReadXmlSchema(schemafilename)
  Dim reader As New Xml.XmlTextReader(dlgopenfile.FileName)
  Dim validatingreader As New Xml.XmlValidatingReader(reader)
  validatingreader.Schemas.Add(Nothing, schemafilename)
  Do While True
  If validatingreader.Read = False Then
  Exit Do
  End If
  Loop
  reader.Close()
  validatingreader.Close()
  newdataset.ReadXml(dlgopenfile.FileName)
  Me.DataGrid1.DataSource = newdataset

  Catch ex As Exception
  If ex.GetType Is GetType(Xml.Schema.XmlSchemaException) Then
  MsgBox("the xml file is not valid:" & ex.Message)
  Else
  MsgBox("a general exception occured:" & ex.Message)
  End If
  End Try
  End If
  End Sub
End Class
可是不管我导入哪一个可以通过IE打开的xml文件,都出现了如下提示:
the xml file is not valid:应为架构根。请确保根元素是<schema>,并且命名空间为“http://www3.org/2001/XMLSchema”(对于XSD架构)或"urn:schemas-microsoft-com:xml-data"(对于XDR架构)
求解答


[解决办法]
Xml.XmlValidatingReader 需要你的XMl架构信息 如果你只需要判断一个XMl是否合法 不需要用这个
直接加载 如果不合法 会加载不成功,不需要这种验证,这种验证和XmlSchemaValidator是一样的东西 ,需要一个架构信息,

就是说你的Xml必须满足一定的架构信息 
主要是:某个元素下面必须出现什么元素,某个元素下面的可能出现的元素 属性的完整性等.

热点排行