By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,505 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,505 IT Pros & Developers. It's quick & easy.

XSD schema does not validate

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.