It does validate. I've just run the code from the sample on device (adjusted for paths and luck of console). Sure enough it shows expected message about attribute. Here’s my code (add two buttons and a textbox to the form, also add XML and XSD files to your project, set action to “Content” and condition to “Deploy always”):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Reflection;
namespace ValidateXml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
// Create and load the XML document.
XmlDocument doc = new XmlDocument();
doc.Load(Path.Combine(path, "booksSchema.xml"));
// Make changes to the document.
XmlElement book = (XmlElement) doc.DocumentElement.FirstChild;
book.SetAttribute("publisher", "Worldwide Publishing");
// Create an XmlNodeReader using the XML document.
XmlNodeReader nodeReader = new XmlNodeReader(doc);
// Set the validation settings on the XmlReaderSettings object.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add("urn:bookstore-schema", Path.Combine(path, "books.xsd"));
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Create a validating reader that wraps the XmlNodeReader object.
XmlReader reader = XmlReader.Create(nodeReader, settings);
// Parse the XML file.
while (reader.Read());
}
// Display any validation errors.
private void ValidationCallBack(object sender, ValidationEventArgs e)
{
this.textBox1.Text = String.Format("Validation Error: {0}", e.Message);
}
}
}
|