Ok I have a class called Product
=============== =============== =============== =
Code: Product.vb
=============== =============== =============== =
Imports System.Xml.Seri alization
<XmlRootAttribu te(ElementName: ="product", [Namespace]:="x")> _
Public Class product
Public _product_name As String
End Class
=============== =============== =============== =
I want to serialize an instance of this class to XML
and validate it against the following product.xsd
saved in the \bin folder
=============== =============== =============== ==
Product.xsd
=============== =============== =============== ==
<?xml version="1.0"?>
<xsd:schema
xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
targetNamespace ="x">
<xsd:element name="Product">
<xsd:complexTyp e>
<xsd:sequence >
<xsd:element name="ProductNa me" type="xsd:strin g" />
</xsd:sequence>
</xsd:complexType >
</xsd:element>
</xsd:schema>
=============== =============== =============== ==
Heres my code thus far
=============== =============== =============== ==
myModule.vb
=============== =============== =============== ==
Imports System.Xml
Imports System.Xml.Sche ma
Imports System.Xml.Seri alization
Imports System.IO
Module myModule
Private isValid As Boolean = True
Sub Main()
Dim xs As XmlSerializer
xs = New XmlSerializer(G etType(product) )
Dim objProduct As New product
objProduct.Prod uctName = "QWERTY"
Dim ms As New MemoryStream
xs.Serialize(ms , objProduct)
Dim xtr As XmlTextReader = _
New XmlTextReader(m s)
Dim xvr As New XmlValidatingRe ader(xtr)
xvr.ValidationT ype = ValidationType. Schema
xvr.Schemas.Add ("x", "Product.xs d")
AddHandler xvr.ValidationE ventHandler, _
AddressOf MyValidationEve ntHandler
Try
While xvr.Read()
End While
Catch ex As Exception
Console.WriteLi ne(ex.Message)
End Try
xvr.Close()
' Check whether the document is valid or invalid.
If (isValid) Then
Console.WriteLi ne("Document is valid")
Else
Console.WriteLi ne("Document is invalid")
End If
End Sub
'============== =============== =============== ===
' Sub: MyValidationEve ntHandler
'============== =============== =============== ===
Public Sub MyValidationEve ntHandler( _
ByVal sender As Object, _
ByVal args As ValidationEvent Args)
isValid = False
Console.WriteLi ne("Validation event\n" + args.Message)
End Sub
End Module
=============== =============== =============== ==
Q1. Am I using the namespaces properly?
Q2. The serialisation to MemoryStream is binary perhaps this is my
problem
Much appreciated if someone could correct this code - or point me in
the direction of a solution