Validating xml with xsd problem.

Hi all, I am trying to validate an xml document which has been created in the dom. Once it has been saved it will validate fine but I need my application to work without any files being physically saved.

Below is a example of what I am trying to do:

SCHEMA:

< xml version="1.0" encoding="utf-8" >
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Schema" targetNamespace="urn:Schema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="root">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="element">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="subElementOne" type="xs:string"/>
       <xs:element name="subElementTwo" type="xs:string"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

CODE:

Imports System
Imports System.Xml
Imports System.Xml.Schema
Module Module1

    Sub Main()

        'DONT WORK!
        '-----------------------------------------------------------------------
        Dim xmlDoc As New XmlDocument
        Dim element As XmlElement
        Dim elementChild As XmlElement
        Dim node As XmlNode

        element = xmlDoc.CreateElement("root")
        node = xmlDoc.AppendChild(element)
        xmlDoc.DocumentElement.SetAttribute("xmlns", "urn:Schema")

        element = xmlDoc.CreateElement("element")
        node = xmlDoc.DocumentElement.AppendChild(element)

        elementChild = xmlDoc.CreateElement("subElementOne")
        node = element.AppendChild(elementChild)

        Dim settings As XmlReaderSettings = New XmlReaderSettings()
        settings.Schemas.Add("urn:Schema", "xsdDoc.xsd")
        settings.ValidationType = ValidationType.Schema
        AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf settingsValidationEventHandler)

        Dim nodeReader As New XmlNodeReader(xmlDoc)
        Dim reader As XmlReader = XmlReader.Create(nodeReader, settings)

        While reader.Read()
        End While
        '-----------------------------------------------------------------------

        Console.Clear()

        'WORKS!
        '-----------------------------------------------------------------------
        xmlDoc.Save("xmlDoc.xml")
        reader = XmlReader.Create("xmlDoc.xml", settings)

        While reader.Read()
        End While
        '-----------------------------------------------------------------------


    End Sub

    Sub settingsValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)

        If e.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
            Console.WriteLine(e.Message)
        ElseIf e.Severity = XmlSeverityType.Error Then
            Console.Write("ERROR: ")
            Console.WriteLine(e.Message)
        End If

    End Sub

End Module


Think Im nearly there but just cant get this last bit cheers, bailey.



Answer this question

Validating xml with xsd problem.

  • nicromi

    Marshes wrote:

    element = xmlDoc.CreateElement("root")
    node = xmlDoc.AppendChild(element)
    xmlDoc.Schemas.Add("urn:Schema", "xsdDoc.xsd")
    xmlDoc.DocumentElement.SetAttribute("xmlns", "urn:Schema")

    element = xmlDoc.CreateElement("element")
    node = xmlDoc.DocumentElement.AppendChild(element)

    elementChild = xmlDoc.CreateElement("subElementOne")
    node = element.AppendChild(elementChild)

    If you want to create an element in a certain namespace then use a namespace aware overload of CreateElement e.g. use

    element = xmlDoc.CreateElement("root", "urn:Schema")

    and

    element = xmlDoc.CreateElement("element", "urn:Schema")

    and

    elementChild = xmlDoc.CreateElement("subElementOne", "urn:Schema")

    The namespace of an element or attribute node is determined when the node is being created, not later by trying to add xmlns attributes or inserting an element.



  • Andrew Stanford

    Marshes wrote:

    Hi all, I am trying to validate an xml document which has been created in the dom. Once it has been saved it will validate fine but I need my application to work without any files being physically saved.

    With .NET 2.0 you have a method named Validate on XmlDocument that you can use to validate the in memory DOM document without needing to save/serialize the document.



  • Mile Petrov

    Hi Martin, I have came across that today and have had no luck with it or finding an example which uses it in this context I have been trying this to no avail any pointers on where im going wrong

    Imports System.Xml
    Imports System.Xml.Schema
    Module Module1

    Sub Main()

    Dim xmlDoc As New XmlDocument
    Dim element As XmlElement
    Dim elementChild As XmlElement
    Dim node As XmlNode

    element = xmlDoc.CreateElement("root")
    node = xmlDoc.AppendChild(element)
    xmlDoc.Schemas.Add("urn:Schema", "xsdDoc.xsd")
    xmlDoc.DocumentElement.SetAttribute("xmlns", "urn:Schema")

    element = xmlDoc.CreateElement("element")
    node = xmlDoc.DocumentElement.AppendChild(element)

    elementChild = xmlDoc.CreateElement("subElementOne")
    node = element.AppendChild(elementChild)

    Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
    xmlDoc.Validate(eventHandler)

    Stop

    End Sub

    Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)

    Select Case e.Severity
    Case XmlSeverityType.Error
    Console.WriteLine("Error: {0}", e.Message)
    Case XmlSeverityType.Warning
    Console.WriteLine("Warning {0}", e.Message)
    End Select

    End Sub

    End Module


  • Wilk06

    One problem with schema validation is that the validator looks at the namespace of the root element of an instance document and looks for a schema defining elements in that namespace. That way if you have a schema defining elements in a certain namespace but you get an XML instance document to validate which has elements in no namespace the schema does not apply. The schema validator will only give a warning if you use the settings to emit warnings. However for the Validate method you cannot specify settings and unfortunately I don't know a solution in this case besides having your own code compare the xmlDocumentInstance.DocumentElement.NamespaceURI to the target namespace of your schema.

  • Tom Frey

    Hi martin thanks alot that has got it going now, however I am curious to know how you would add a schema to an xml document passed to a function from an external source.

    Say you are sent an xml document which has no namespaces etc and you have an xsd that you want to validate it against. Would you have to interop using strings and then validate through the XmlReader or is there a way to add the namespace in afterwards and still use xmlDocument.Validate


  • Validating xml with xsd problem.