topical media & game development
professional-xml-15-Listing-15-17.txt / txt
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace XmlProject
{
class Program
{
static void Main(string[] args)
{
try
{
XmlSchemaSet mySchema = new XmlSchemaSet();
mySchema.Add(null, "C:/MyXml.xsd");
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
settings.CheckCharacters = true;
settings.Schemas.Add(mySchema);
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new
ValidationEventHandler(settings_ValidationEventHandler);
FileStream myStockOrders = new
FileStream("C:/MyXml.xml", FileMode.Open);
XmlReader xr = XmlReader.Create(myStockOrders, settings);
while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Element &&
"Symbol" == xr.LocalName)
{
Console.WriteLine(xr.Name + " " +
xr.ReadElementContentAsString());
}
}
xr.Close();
Console.WriteLine("Done");
Console.ReadLine();
}
catch (System.Exception ex)
{
Console.Error.WriteLine(ex.ToString());
Console.ReadLine();
}
}
static void settings_ValidationEventHandler(object sender,
ValidationEventArgs e)
{
throw new Exception("Your XML is invalid.");
}
}
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.