473,320 Members | 1,856 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.

XSD schema does not validate

Hi,

I use C# to validate an XML document. It issues error messages and I do not
understand why.

Here is the XML document:
<bookstore xmlns="http://tempuri.org/books.xsd">

<book genre="novel" style="hardcover">

<title>The Handmaid's Tale</title>

<price>19.95</price>

<author>

<first-name>Margaret</first-name>

<last-name>Atwood</last-name>

</author>

</book>

<book genre="novel" style="paperback">

<title>The Poisonwood Bible</title>

<price>11.99</price>

<author>

<first-name>Barbara</first-name>

<last-name>Kingsolver</last-name>

</author>

</book>

<book genre="novel" style="hardcover">

<title>Hannibal</title>

<price>27.95</price>

<author>

<first-name>Richard</first-name>

<last-name>Harris</last-name>

</author>

</book>

</bookstore>

I import it in the C# solution and I create from the XML Menu the Schema:

<?xml version="1.0" ?>

<xs:schema id="bookstore" targetNamespace="http://tempuri.org/books.xsd"
xmlns:mstns="http://tempuri.org/books.xsd"
xmlns="http://tempuri.org/books.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">

<xs:element name="bookstore" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">

<xs:complexType>

<xs:choice maxOccurs="unbounded">

<xs:element name="book">

<xs:complexType>

<xs:sequence>

<xs:element name="title" type="xs:string" minOccurs="0" msdata:Ordinal="0"
/>

<xs:element name="price" type="xs:string" minOccurs="0" msdata:Ordinal="1"
/>

<xs:element name="author" minOccurs="0" maxOccurs="unbounded">

<xs:complexType>

<xs:sequence>

<xs:element name="first-name" type="xs:string" minOccurs="0" />

<xs:element name="last-name" type="xs:string" minOccurs="0" />

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

<xs:attribute name="genre" form="unqualified" type="xs:string" />

<xs:attribute name="style" form="unqualified" type="xs:string" />

</xs:complexType>

</xs:element>

</xs:choice>

</xs:complexType>

</xs:element>

</xs:schema>

When I try to run the application the handler returns error messages for
each line from the XML document.

It starts with:

Could not find schema information for the element
http://tempuri.org/books.xsd:bookstore
An error occured at file:///C:/Applications/....../books.xml(1,2)

And this is the code:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Xml.Schema;

using System.Xml.Serialization;

using System.Xml.Xsl;

using System.Xml;

namespace CS_XML_XSD_Schema

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button btnValidate;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.btnValidate = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// btnValidate

//

this.btnValidate.Location = new System.Drawing.Point(158, 139);

this.btnValidate.Name = "btnValidate";

this.btnValidate.TabIndex = 0;

this.btnValidate.Text = "Validate";

this.btnValidate.Click += new System.EventHandler(this.btnValidate_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.btnValidate});

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

///

public static void MyValidationEventHandler(object sender,

ValidationEventArgs args)

{

isValid = false;

MessageBox.Show(args.Message);

// Console.WriteLine("Validation event\n" + args.Message);

}

private static bool isValid = true; // If a validation error occurs,

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void btnValidate_Click(object sender, System.EventArgs e)

{

XmlTextReader r = new XmlTextReader(@"C:\Applications\C
Sharp\My_Tests\CS_XML_XSD_Schema\books.xml");

XmlValidatingReader v = new XmlValidatingReader(r);

v.ValidationType = ValidationType.Schema;

v.ValidationEventHandler +=

new ValidationEventHandler(MyValidationEventHandler);

while (v.Read())

{

// Can add code here to process the content.

}

v.Close();

// Check whether the document is valid or invalid.

if (isValid)

MessageBox.Show(this,"Document is valid");

// Console.WriteLine("Document is valid");

else

MessageBox.Show(this,"Document is invalid");

// Console.WriteLine("Document is invalid");

}

}

}

Any hints please?

Thanks

Doru
Mar 30 '06 #1
2 2831


Doru Roman wrote:

I use C# to validate an XML document. It issues error messages and I do not
understand why. I import it in the C# solution and I create from the XML Menu the Schema:

<?xml version="1.0" ?>

<xs:schema id="bookstore" targetNamespace="http://tempuri.org/books.xsd"
xmlns:mstns="http://tempuri.org/books.xsd"
xmlns="http://tempuri.org/books.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">
Where does you C# application load that schema?

XmlTextReader r = new XmlTextReader(@"C:\Applications\C
Sharp\My_Tests\CS_XML_XSD_Schema\books.xml");

XmlValidatingReader v = new XmlValidatingReader(r);

v.ValidationType = ValidationType.Schema;

v.ValidationEventHandler +=

new ValidationEventHandler(MyValidationEventHandler);

You need something like
v.Schemas.Add("http://tempuri.org/books.xsd", "books.xsd");
where of course the second argument might need some file path besides
that file name, depending on where your schema is stored.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 30 '06 #2
Thanks a lot, that did the job.
I got the example from a site, it seems that not always they are reliable.

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...


Doru Roman wrote:

I use C# to validate an XML document. It issues error messages and I do
not understand why.

I import it in the C# solution and I create from the XML Menu the Schema:

<?xml version="1.0" ?>

<xs:schema id="bookstore" targetNamespace="http://tempuri.org/books.xsd"
xmlns:mstns="http://tempuri.org/books.xsd"
xmlns="http://tempuri.org/books.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">


Where does you C# application load that schema?

XmlTextReader r = new XmlTextReader(@"C:\Applications\C
Sharp\My_Tests\CS_XML_XSD_Schema\books.xml");

XmlValidatingReader v = new XmlValidatingReader(r);

v.ValidationType = ValidationType.Schema;

v.ValidationEventHandler +=

new ValidationEventHandler(MyValidationEventHandler);

You need something like
v.Schemas.Add("http://tempuri.org/books.xsd", "books.xsd");
where of course the second argument might need some file path besides that
file name, depending on where your schema is stored.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Mar 30 '06 #3

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

Similar topics

1
by: eXavier | last post by:
Hi, I need to validate XML fragment against XSD schema. The main issue is that xml fragment does not contain refrence to schema, but I want to force the validation against the schema I have in...
1
by: Ryan | last post by:
I have a very complex XDR schema that uses namespaces: xmlns="urn:schemas-microsoft-com:xml-data" xmlns:b="urn:schemas-microsoft-com:BizTalkServer" xmlns:d="urn:schemas-microsoft-com:datatypes"...
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...
1
by: Dan Bass | last post by:
There's an XML message I have, that has no namespace information. Then there is a XSD schema that is must validate against, but this has a targetNamespace and xmlns of...
6
by: LesleyW | last post by:
Hi Apologies if this is a really dumb question, but being new to XML and Schemas, I wonder if giving the namespace for eg xsd or xsi as a website address means that the user has to be online...
3
by: kkao77 | last post by:
I am trying to use schema to validate the data that user sent to my service. How do I achieve that using schema? Do I give schema to the client? or do I write my own schema validation inside web...
2
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a...
2
by: bmichel | last post by:
Regarding the XSD schema shown below, I want to modifiy it so that: - the "owner_id" attribute in the "dog" element to exist in one of the "owner" element "id" attribute. - the "id" attribute in...
3
by: Lord0 | last post by:
I *think* I need to be able to validate subsets of an XML document using different schema. The functionality I'm trying to implement is this. a) External suppliers produce an XML document...
4
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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.