Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2006

Tip: Looking for answers? Try searching our database.

XSD schema does not validate

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doru Roman - 30 Mar 2006 17:12 GMT
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
Martin Honnen - 30 Mar 2006 17:36 GMT
> 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:
>
[quoted text clipped - 6 lines]
> 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");
[quoted text clipped - 6 lines]
>
> 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.

Signature

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

Doru Roman - 30 Mar 2006 17:51 GMT
Thanks a lot, that did the job.
I got the example from a site, it seems that not always they are reliable.

>> I use C# to validate an XML document. It issues error messages and I do
>> not understand why.
[quoted text clipped - 27 lines]
> where of course the second argument might need some file path besides that
> file name, depending on where your schema is stored.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.