473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Validating an xml document against xsd schema

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--------------------------------------------------
Nov 12 '05 #1
0 1940

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Will | last post by:
I have been having problems validating an XForms document against the XForms schema located at http://www.w3.org/MarkUp/Forms/2002/XForms-Schema.xsd. I have reduced the XForm to its bare bones...
4
by: billcoumbe | last post by:
any recommendations? I'm looking for something that will just run from the unix command line to validate large (20-50Mb) XML files against an XML DTD. Ideally something that is actively...
1
by: Christian | last post by:
Hi, I load an Xml-file "customers.xml" into a DataSet (works fine) but then how do I validate it against a schema (e.g. customers.xsd) ? my customers.xml: <?xml version="1.0"...
2
by: simon ames | last post by:
I have an XML file i'd like to validate against an XSD schema. I'm creating the XML on the fly in an ASP.NET page and need to validate it aginst the schema to ensure its well formed. How do i...
3
by: AP | last post by:
Hello, I'm trying to use .NET to validate incoming XML documents against the appropriate schema in our database. I do not want to use the document's schemaLocation attribute to validate for...
3
by: Shailendra Batham | last post by:
hi guys I need your suggestions / opinion for doing this the right way. I have a XML and a Schema for the same What I want is when its validated against the schema, it should give custom...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: srujana | last post by:
Hi I am validating an xml document against schema . when i am passing the xml file location as an input to the parse method String XmlDocumentUrl="E:/books.xml"; parser.parse(XmlDocumentUrl);...
2
by: Ian | last post by:
I've inherited some xml that doesn't have a namespace and doesn't have a reference to the schema. The schema doesn't have a targetNamespace attribute. I'm trying to build a test tool, in C#, which...
1
by: Chris Lieb | last post by:
I have an XML Schema file that I know is correct becuase I currently use it in a VB6 program to validate XML documents. Also, if I load an XML file into VS2005 that is not valid against this...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.