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