EDN Admin
Well-known member
Hello, just getting started with XML files and the validation of them, using VS2010, Windows Forms and VB.Net.
I created a simple xml file programmatically
rivate Sub SaveFilter()
Dim serializer As New XmlSerializer(GetType(List(Of FilterRow)))
Dim stream = New StreamWriter("C:Tempfilter.xml")
Using stream
serializer.Serialize(stream, m_RowCollection)
End Using
End Sub
m_RowCollection is a List(Of FilterRow)
FilterRow is a simple class
ublic Class FilterRow
Public RowIndex As Integer
Public ColumnName As String
Public Operand As String
Public Value As String
Public Conjunction As String
End Class
So I populate an instance of FilterRow and throw it into an xml file using the above SaveFilter method. The resulting file:<?xml version="1.0" encoding="utf-8"?>
<ArrayOfFilterRow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema
<FilterRow>
<RowIndex>0</RowIndex>
<ColumnName>Driver Code</ColumnName>
<Operand>Equals</Operand>
<Value>OPTA</Value>
<Conjunction>End</Conjunction>
</FilterRow>
</ArrayOfFilterRow>
Then I opened the xml file in VS2010 and, using the XML Editor Toolbar, clicked Create Schema. The resulting xsd file:<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified
<xsd:element name="ArrayOfFilterRow
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FilterRow
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RowIndex" type="xsd:unsignedByte" />
<xsd:element name="ColumnName" type="xsd:string" />
<xsd:element name="Operand" type="xsd:string" />
<xsd:element name="Value" type="xsd:string" />
<xsd:element name="Conjunction" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xs:schema>
I then attempted to validate the xml file against the xsd file. This resulted in an error, written to console: "ERROR: The ArrayOfFilterRow element is not declared."
Im validating with the following routines:
In a Form
im validator = New XMLValidation
validator.Validate(path, validator.GetSandGridFilterSchema)
...where path = the full path to the xml file Im retrieving from disk.
And the class I use to perform validation in:Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Public Class XMLValidation
Public Sub Validate(ByVal xmlPath As String, ByVal xsd As XmlSchema)
Dim settings = New XmlReaderSettings
settings.Schemas.Add(xsd)
settings.ValidationType = ValidationType.Schema
AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf XMLValidationEventHandler)
Dim reader = XmlReader.Create(xmlPath, settings)
While reader.Read
End While
End Sub
Private Sub XMLValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Warning
Console.Write("WARNING: ")
Console.WriteLine(e.Message)
Case XmlSeverityType.Error
Console.Write("ERROR: ")
Console.WriteLine(e.Message)
End Select
End Sub
Public Function GetSandGridFilterSchema() As XmlSchema
Dim reader = New XmlTextReader(New StringReader(My.Resources.SandGridFilterSchemaDefinition))
Dim schema As New XmlSchema
schema = XmlSchema.Read(reader, AddressOf XMLValidationEventHandler)
Return schema
End Function
End Class
Can anyone tell me why this simple arrangement fails, please? I dont understand it, because the ArrayOfFilterRow element is declared.
Thanks for any help.
View the full article
I created a simple xml file programmatically

Dim serializer As New XmlSerializer(GetType(List(Of FilterRow)))
Dim stream = New StreamWriter("C:Tempfilter.xml")
Using stream
serializer.Serialize(stream, m_RowCollection)
End Using
End Sub
m_RowCollection is a List(Of FilterRow)
FilterRow is a simple class

Public RowIndex As Integer
Public ColumnName As String
Public Operand As String
Public Value As String
Public Conjunction As String
End Class
So I populate an instance of FilterRow and throw it into an xml file using the above SaveFilter method. The resulting file:<?xml version="1.0" encoding="utf-8"?>
<ArrayOfFilterRow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema
<FilterRow>
<RowIndex>0</RowIndex>
<ColumnName>Driver Code</ColumnName>
<Operand>Equals</Operand>
<Value>OPTA</Value>
<Conjunction>End</Conjunction>
</FilterRow>
</ArrayOfFilterRow>
Then I opened the xml file in VS2010 and, using the XML Editor Toolbar, clicked Create Schema. The resulting xsd file:<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified
<xsd:element name="ArrayOfFilterRow
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FilterRow
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RowIndex" type="xsd:unsignedByte" />
<xsd:element name="ColumnName" type="xsd:string" />
<xsd:element name="Operand" type="xsd:string" />
<xsd:element name="Value" type="xsd:string" />
<xsd:element name="Conjunction" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xs:schema>
I then attempted to validate the xml file against the xsd file. This resulted in an error, written to console: "ERROR: The ArrayOfFilterRow element is not declared."
Im validating with the following routines:
In a Form

validator.Validate(path, validator.GetSandGridFilterSchema)
...where path = the full path to the xml file Im retrieving from disk.
And the class I use to perform validation in:Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Public Class XMLValidation
Public Sub Validate(ByVal xmlPath As String, ByVal xsd As XmlSchema)
Dim settings = New XmlReaderSettings
settings.Schemas.Add(xsd)
settings.ValidationType = ValidationType.Schema
AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf XMLValidationEventHandler)
Dim reader = XmlReader.Create(xmlPath, settings)
While reader.Read
End While
End Sub
Private Sub XMLValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Warning
Console.Write("WARNING: ")
Console.WriteLine(e.Message)
Case XmlSeverityType.Error
Console.Write("ERROR: ")
Console.WriteLine(e.Message)
End Select
End Sub
Public Function GetSandGridFilterSchema() As XmlSchema
Dim reader = New XmlTextReader(New StringReader(My.Resources.SandGridFilterSchemaDefinition))
Dim schema As New XmlSchema
schema = XmlSchema.Read(reader, AddressOf XMLValidationEventHandler)
Return schema
End Function
End Class
Can anyone tell me why this simple arrangement fails, please? I dont understand it, because the ArrayOfFilterRow element is declared.
Thanks for any help.
View the full article