Validating against a custom schema

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Ross
    New Member
    • Jan 2007
    • 119

    Validating against a custom schema

    I am simply playing around with xml schemas to understand them better and came across this article -


    The trouble is in this example no matter what changes I make to the XML document it never raises the validationEvent Handler.

    Here is my code -
    Code:
       
    Public Shared Sub ValTest()
                Dim sc As XmlSchemaSet = New XmlSchemaSet()
    
                ' Add the schema to the collection.
                sc.Add("urn:bookstore-schema", System.Web.Hosting.HostingEnvironment.MapPath("~\UserConfigSchema.xsd"))
    
                ' Set the validation settings.
                Dim settings As XmlReaderSettings = New XmlReaderSettings()
                settings.ValidationType = ValidationType.Schema
                settings.Schemas = sc
                AddHandler settings.ValidationEventHandler, AddressOf ValidationEventHandler
    
                ' Create the XmlReader object.
                Dim reader As XmlReader = XmlReader.Create(System.Web.Hosting.HostingEnvironment.MapPath("~\Default Profiles\SE.xml"), settings)
    
                ' Parse the file. 
                While reader.Read()
                End While
            End Sub
    
            Private Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
                'Just for testing
                Dim obj = sender
                Dim obj2 = args
            End Sub
    The namespaces are correct and I can assure you that SE.xml should not validate against UserConfigSchem a.xsd as I have thrown in some random new elements.

    Can anyone point in the righ direction? What am I doing wrong?
  • Bob Ross
    New Member
    • Jan 2007
    • 119

    #2
    Typical I spend ages trying to get this to work, fail, so post it on here and it works.
    I tihnk all I needed to do was to remove the code from an existing sub and place in a new one. Which I did to make it readable on here and now it works.
    Oh well.

    Comment

    Working...