473,386 Members | 1,943 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,386 software developers and data experts.

XML validation Problem

Hi,
My code won't validate the xml document.
I'm using vb.net.
In the IDE I can use the schema to validate the XML. But when I use
XmlValidatingReader in code it does not pick any restrictions.

eg. in the following schema I want one and only one 'Header' element,
and I want a 'Message' element to be within a Messages element as
'Messages' is a collection of 'Message'.
If I change these in the IDE (when I've pulled them into the project)
the validation works. How can I do this through code.

Thanks in advance.
XSD File
---------
<?xml version="1.0" encoding="utf-8"?>
<schema id="DataTransfer" targetNamespace="http://www.Test.com"
xmlns:idtdt="http://www.Test.com"
xmlns="http://www.w3.org/2001/XMLSchema">
<!-- PACKAGE DOCUMENT -->
<element name="Doc">
<complexType>
<!-- Header Information -->
<sequence>
<element name="Header" maxOccurs="1" minOccurs="1">
<complexType>
<attribute name="FileName" type="string"
use="required" />
</complexType>
</element>
<!-- A collection of zero or more Messages -->
<element name="Messages" maxOccurs="1">
<complexType>
<sequence>
<!-- A Message -->
<element name="Message"
maxOccurs="unbounded">
<complexType>
<sequence>
<!-- A collection of zero or
more Parameters -->
<element name="Value"
type="string" />
</sequence>
<attribute name="MessageID"
type="string" use="required" />
</complexType>
</element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
<key name="PackageDocKey1">
<selector xpath=".//Message" />
<field xpath="@MessageID" />
</key>
<key name="PackageDocKey2">
<selector xpath="./Messages/Parameters/Parameter" />
<field xpath="@Name" />
</key>
</element>
<!-- Process Status Enumeration -->
</schema>


XML File
--------

<?xml version="1.0" encoding="utf-8" ?>
<Doc xmlns="http://www.Test.com">
<!-- Header is a once only entry -->
<Header FileName ="ZZZ.txt"></Header>
<!-- Messages is a collection of (0 or many) Message -->
<Messages>
<Message MessageID ="1">
<!-- Value is a once only entry -->
<Value>TEST1</Value>
</Message>
<Message MessageID ="2">
<Value>TEST4</Value>
</Message>
<Message MessageID ="3">
<Value>TEST3</Value>
</Message>
</Messages>
</Doc>
Nov 11 '05 #1
3 2587
paul wrote:
My code won't validate the xml document.
I'm using vb.net.
In the IDE I can use the schema to validate the XML. But when I use
XmlValidatingReader in code it does not pick any restrictions.


Most likely you have some mistake in your VB code. Show us relevaln
tpart and make sure you have read "Validation of XML with
XmlValidatingReader" at
http://msdn.microsoft.com/library/de...tingreader.asp

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
Yes I've read the site.

'MY CODE

Option Strict On
Option Explicit On

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports Microsoft.VisualBasic

Public Class SchemaCollectionSample
Private doc1 As String = "test.xml"
Private reader As XmlTextReader = Nothing
Private vreader As XmlValidatingReader = Nothing
Private m_success As Boolean = True
Public Shared Sub Main()
Dim validation As New SchemaCollectionSample
End Sub 'Main

Public Sub New()
'Load the schema collection
Dim xsc As New XmlSchemaCollection
Try
xsc.Add("http://www.Test.com", New XmlTextReader("TestSchema.xsd"))
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
'Validate the files using schemas stored in the collection.
Validate(doc1, xsc) 'Should pass.
End Sub 'New

Private Sub Validate(ByVal filename As String, ByVal xsc As XmlSchemaCollection)
m_success = True
Console.WriteLine()
Console.WriteLine("Validating XML file {0}...", filename.ToString())
reader = New XmlTextReader(filename)
'Create a validating reader.
vreader = New XmlValidatingReader(reader)

'Use the schemas stored in the schema collection.
vreader.Schemas.Add(xsc)
Try
'Read and validate the XML data.
While vreader.Read()
End While
Catch xmlEx As XmlException
Console.WriteLine(xmlEx.ToString())
Finally
'Close the reader.
vreader.Close()
End Try
End Sub
End Class
Nov 11 '05 #3
Now I see.
First of all your test.xml is invalid against the schema. Both XML Spy
and your code throw validation error:

Validating XML file test.xml...

Unhandled Exception: System.Xml.Schema.XmlSchemaException: The element
'http://www.Test.com:Doc' has invalid child element
'http://www.Test.com:Header'. Expected 'Header'.

That's becuase your schema definition by default define all locally
defined elements to be unqualified. See
http://www.w3.org/TR/xmlschema-0/#UnqualLocals for more info.
To make it valid add elementFormDefault="qualified" to schema element:

<schema id="DataTransfer" targetNamespace="http://www.Test.com"
xmlns:idtdt="http://www.Test.com"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4

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

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
41
by: Gérard Talbot | last post by:
Cross-posted to: comp.infosystems.www.authoring.html and alt.html Followup-to: comp.infosystems.www.authoring.html 1- One day, I stumbled across a website that offers to validate webpages. What...
3
by: Kent Ogletree | last post by:
I am porting a Java XML Validation class over to C# and I am having a problem finding exactly what I need to acomplish the task. First I need to test for well formedness. I know this is usually...
4
by: | last post by:
Hello Guys, I am using the validation controls to validate my data. But the problem is "The page is still being posted to server". I want to get rid of the round trips to server. Are there...
4
by: usl2222 | last post by:
Hi folks, I appreciate any assistance in the following problem: I have a form with a bunch of dynamic controls on it. All the controls are dynamically generated on a server, including all...
3
by: john morales | last post by:
Hi guys, I have a problem and i know there must be a solution for this as it is such a basic common practice in asp.net development. Scenario: i have many webforms in a site, most with two...
5
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
4
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
9
by: Bill Long | last post by:
I have a control that simply displays a list of links. Following one of the links doesn't post back or redirect to another page, it simply hides the current panel and shows the one you selected......
2
by: dustbort | last post by:
I recently had a problem where my required field validator stopped working. But, the page still posted back and tried to insert a record into the database without performing server-side validation....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.