DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Validating XML fragment against schema

  1. #1
    Join Date
    Jul 2005

    Question Validating XML fragment against schema


    I am finding diffcult to validate a xml fragment to validate against a schema.
    The situation is as follows:

    I am making an xml string on the fly, but before submitting it to another server, I need to validate that it is a valid xml against schema. I have the schema saved locally on the server. Here is the xml:
    <?xml version='1.0' encoding='UTF-8'?>
    <portalReq xmlns=''>
    <items><item itemNumber='1'>
    <accountName>Mr. A J Kaul</accountName>
    I have attached the schema file as .txt with this post.
    I have come across XmlValidatingReader class and tried to use it, but even if I take out one required element from xml, it does not produce error: Following is the code:

    Private Function ValidateXML(ByVal objStr As StringReader)
            Dim reader As XmlValidatingReader = Nothing
            Dim myschema As New XmlSchemaCollection
                'Create the XML fragment to be parsed.
                Dim xmlFrag As String = objStr.ReadToEnd
                'Create the XmlParserContext.
                Dim context As New XmlParserContext(Nothing, Nothing, "", XmlSpace.None)
                'Implement the reader.
                reader = New XmlValidatingReader(xmlFrag, XmlNodeType.Element, context)
                'Add the schema.
                Dim strXSD As String = Server.MapPath("abc.xsd")
                myschema.Add("", strXSD)
                'Set the schema type and add the schema to the reader.
                reader.ValidationType = ValidationType.Schema
                'Add the handler to raise the validation event.
                AddHandler reader.ValidationEventHandler, AddressOf Me.ValidationEventHandle
                While reader.Read
                End While
                Label1.Text = Label1.Text + "Completed validating xmlfragment"
            Catch XmlExp As XmlException
                Label1.Text = Label1.Text + XmlExp.Message
            Catch XmlSchExp As XmlSchemaException
                Label1.Text = Label1.Text + XmlSchExp.Message
            Catch GenExp As Exception
                Label1.Text = Label1.Text + GenExp.Message
            End Try
        End Function
        Public Sub ValidationEventHandle(ByVal sender As Object, ByVal args As ValidationEventArgs)
            m_success = False
            Console.WriteLine((ControlChars.CrLf & ControlChars.Tab & "Validation error: " & args.Message))
        End Sub 'ValidationEventHandle
        Private Function validateXML(ByVal xml As String)
                'Is the xml string valid?
                If (xml Is Nothing Or xml.Length < 1) Then
                    Return False
                End If
                Dim srXml As StringReader = New StringReader(xml)
                Return validateXML(srXml)
            Catch ex As Exception
                ValidationError = ex.Message
                Return False
            End Try
        End Function
        Public Property ValidationError()
                Return "<ValidationError>" + ValidationError + "</VALIDATIONERROR>"
            End Get
            Set(ByVal Value)
                ValidationError = Value
            End Set
        End Property
        Public Sub validationCallBack(ByVal sender As Object, ByVal args As ValidationEventArgs)
            Label1.Text = "Error:" + args.Message
        End Sub
    Please help!!
    Attached Files Attached Files

Similar Threads

  1. Replies: 0
    Last Post: 10-28-2002, 07:25 AM
  2. Validating an XML using a schema
    By AnNic in forum XML
    Replies: 0
    Last Post: 03-09-2002, 04:27 AM
  3. validating xml against an inline schema
    By shalini in forum .NET
    Replies: 0
    Last Post: 02-06-2002, 04:39 AM
  4. Replies: 0
    Last Post: 10-05-2001, 05:08 AM
  5. Replies: 4
    Last Post: 07-05-2001, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center