Imports System.Xml.Schema
Imports System.Xml.Serialization
Imports System.Xml
Public Class CommonUtil
    Friend Shared Function SerializeObjectToXML(ByVal obj As Object, ByVal tp As Type) As String
        Dim oXS As XmlSerializer = New XmlSerializer(tp)
        Dim xw As Xml.XmlWriter
        Dim output As New System.Text.StringBuilder
        Try
            xw = Xml.XmlWriter.Create(output)
            oXS.Serialize(xw, obj)
            Return (output.ToString())
        Catch ex As Exception
            Throw ex

        Finally

        End Try
    End Function
    Friend Shared Function ValidateXML(ByVal strXML) As Boolean
        Dim xrs As XmlReaderSettings = New XmlReaderSettings()
        Dim objXML As System.IO.StringReader
        Dim objXMLDOM As New XmlDataDocument()
        Try

            xrs.ValidationType = ValidationType.Schema
            xrs.ValidationFlags = xrs.ValidationFlags Or XmlSchemaValidationFlags.ProcessInlineSchema
            xrs.ValidationFlags = xrs.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
            'xrs.Schemas.Add(Nothing, xr)
            xrs.Schemas.Add(Nothing, "C:\GetServiceabilityRequest.xsd")
            'Add Validation handlers to XML Schema
            AddHandler xrs.ValidationEventHandler, AddressOf ValidationEventHandle
            objXML = New System.IO.StringReader(strXML)
            Dim reader As XmlReader = XmlReader.Create(objXML, xrs)
            'Load XML DOM using reader
            objXMLDOM.Load(reader)
            Return True

        Catch ex As Exception
            Throw ex
        Finally


        End Try
    End Function
    Friend Shared Sub ValidationEventHandle(ByVal sender As Object, ByVal args As ValidationEventArgs)
        Try
            Throw New Exception(args.Message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class
