Hello Everyone,
I'm sure this question has been asked a million times, but I'm going
crazy trying to get my xml validation to work. I've spent the day
reading msdn, the dot net documentation and samples found on the web,
and I'm still having no luck. Basically, the file always loads
without error, even though it shouldn't be valid. Here are my various
files, any help is greatly appreciated. There are some comments
inline.
thanks so much
Schema--------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema
targetNamespace="test"
xmlns="test"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" >
<xs:element name="Seat" type="typeSeat"/>
<xs:complexType name="typeSeat">
<xs:sequence>
<xs:element name="Application" minOccurs="1" maxOccurs="1"
type="typeNameIdPair"/>
<xs:element name="Language" minOccurs="1" maxOccurs="1"
type="typeNameIdPair"/>
<xs:element name="Components" minOccurs="0" maxOccurs="1"
type="typeComponents"/>
</xs:sequence>
<xs:attribute name="ConfigurationCode" type="typeConfigCode"
use="optional"/>
<xs:attribute name="Code" type="typeSeatCode" use="optional"/>
<xs:attribute name="ComputerCode" type="typeComputerCode"
use="optional"/>
<xs:attribute name="KeyCode" type="typeKeyCode" use="optional"/>
</xs:complexType>
<xs:simpleType name="typeConfigCode">
<xs:restriction base="xs:string">
<xs:length value="33" />
<xs:pattern value="/d{5}-/d{9}-/d{3}" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="typeSeatCode">
<xs:restriction base="xs:string">
<xs:length value="15" />
<xs:pattern value="/d{4}-/d{4}-/d{4}" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="typeKeyCode">
<xs:restriction base="xs:string">
<xs:length value="12" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="typeComputerCode">
<xs:restriction base="xs:string">
<xs:length value="6" />
<xs:pattern value="COMPU/d{6}" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="typeComponents">
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="Component" type="typeNameIdPair"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="typeNameIdPair">
<xs:sequence>
<xs:element name="Name" minOccurs="1" maxOccurs="1"/>
<xs:element name="Id" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
//Schema--------------------------------------------------
This should fail the validation because the computer id attribute is
invalid (it should be 'COMPU' + 6digits)
XML Doc --------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<test:Seat
xmlnsxsi="http//www.w3.org/2001/XMLSchema-instance"
xmlns:test="www.mytest.com/schemas/Seat"
xsischemalLocation="http//localhost/schemas/Seat.xsd"
ConfigurationCode="28-1821569D-1BB32FC416080FEB-0539"
Id="01-538-10844280" KeyCode="SRRZ8382MKUZ" ComputerId="test"
<Application>
<Name>My Application</Name>
<Id>1</Id>
</Application>
<Language>
<Name>English</Name>
<Id>1</Id>
</Language>
<Components>
<Component>
<Name>Some Dll</Name>
<Id>3</Id>
</Component>
<Component>
<Name>Some Dll</Name>
<Id>2</Id>
</Component>
</Components>
</test:Seat>
//XML Doc--------------------------------------------------
The code is pretty much right out of the msdn documentation. I copied
their sample files, and ran the provided code, and it didn't work
either. oVal.SchemaType is always nothing.
VB Code--------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oRdr As New Xml.XmlTextReader("http://localhost/seat.xml")
Dim oVal As New Xml.XmlValidatingReader(oRdr)
oVal.ValidationType = Xml.ValidationType.Schema
AddHandler oVal.ValidationEventHandler, AddressOf
ValidationHandler
Try
oVal.Schemas.Add("actSeat",
"http://localhost/schemas/seat.xsd")
oDoc.Load(oVal)
Catch ex As System.Exception
TextBox1.Text += Ex.Message + "( " + Ex.Source + ") " +
vbCrLf
Exit Sub
End Try
Try
While oVal.Read()
PrintTypeInfo(oVal)
If oVal.NodeType = XmlNodeType.Element Then
While oVal.MoveToNextAttribute()
PrintTypeInfo(oVal)
End While
End If
End While
Catch Ex As System.Exception
TextBox1.Text += Ex.Message + "( " + Ex.Source + " )"
End Try
oVal.Close()
oRdr.Close()
End Sub
Public Shared Sub PrintTypeInfo(ByVal vr As XmlValidatingReader)
If Not (vr.SchemaType Is Nothing) Then
If TypeOf vr.SchemaType Is XmlSchemaDatatype Or TypeOf
vr.SchemaType Is XmlSchemaSimpleType Then
Dim value As Object = vr.ReadTypedValue()
Console.WriteLine("{0}({1},{2}):{3}", vr.NodeType,
vr.Name, value.GetType().Name, value)
Else
If TypeOf vr.SchemaType Is XmlSchemaComplexType Then
Dim sct As XmlSchemaComplexType =
CType(vr.SchemaType, XmlSchemaComplexType)
Console.WriteLine("{0}({1},{2})", vr.NodeType,
vr.Name, sct.Name)
End If
End If
End If
End Sub
End Class
//VB Code--------------------------------------------------