2011年7月9日 星期六

[ Asp.Net ] 以ashx上傳XML檔案(不存檔)配合xsd驗證及讀取資料

程式碼片段  LoadXm..ashx

           '以 StreamReader 取得該檔案!
            Dim XmlFile As New System.IO.StreamReader(context.Request.Files(0).FileName)
            Dim xmlContent As String = XmlFile.ReadToEnd()
            Dim xmlStringReader As System.IO.StringReader = New System.IO.StringReader(xmlContent)



           ' 建立 XmlSchemaSet
            Dim sc As XmlSchemaSet = New XmlSchemaSet()
            sc.Add("", context.Server.MapPath("Your.xsd"))
            '設定validation settings.
            Dim settings As XmlReaderSettings = New XmlReaderSettings()
            settings.ValidationType = ValidationType.Schema
            settings.Schemas = sc
            AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
           
           '驗證上傳後的檔案
            Dim reader As XmlReader = XmlReader.Create(xmlStringReader, settings)
             While reader.Read()
                    ......................
            End While

沒有留言:

張貼留言

追蹤者