This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Xml; | |
using System.Xml.Schema; | |
using System.Xml.XPath; | |
namespace XSD_fun | |
{ | |
class Program | |
{ | |
private static string xml = | |
@"<?xml version=""1.0"" encoding=""utf-8"" ?> | |
<bookstore xmlns = ""http://www.contoso.com/books""> | |
<book genre=""autobiography"" publicationdate=""1981-03-22"" ISBN=""1-861003-11-0""> | |
<title>The Autobiography of Benjamin Franklin</title> | |
<author> | |
<first-name>Benjamin</first-name> | |
<last-name>Franklin</last-name> | |
</author> | |
<price>8.99</price> | |
</book> | |
<book genre = ""novel"" publicationdate=""1967-11-17"" ISBN=""0-201-63361-2""> | |
<title>The Confidence Man</title> | |
<author> | |
<first-name>Herman</first-name> | |
<last-name>Melville</last-name> | |
</author> | |
<price>11.99</price> | |
</book> | |
<book genre = ""philosophy"" publicationdate=""1991-02-15"" ISBN=""1-861001-57-6""> | |
<title>The Gorgias</title> | |
<author> | |
<name>Plato</name> | |
</author> | |
<price>9.99</price> | |
</book> | |
</bookstore>"; | |
private static string xsd = | |
@"<?xml version=""1.0"" encoding=""utf-8""?> | |
<xs:schema attributeFormDefault = ""unqualified"" elementFormDefault=""qualified"" targetNamespace=""http://www.contoso.com/books"" xmlns:xs=""http://www.w3.org/2001/XMLSchema""> | |
<xs:element name = ""bookstore""> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element maxOccurs = ""unbounded"" name=""book""> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element name = ""title"" type=""xs:string"" /> | |
<xs:element name = ""author""> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element minOccurs = ""0"" name=""name"" type=""xs:string"" /> | |
<xs:element minOccurs = ""0"" name=""first-name"" type=""xs:string"" /> | |
<xs:element minOccurs = ""0"" name=""last-name"" type=""xs:string"" /> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:element> | |
<xs:element name = ""price"" type=""xs:decimal"" /> | |
</xs:sequence> | |
<xs:attribute name = ""genre"" type=""xs:string"" use=""required"" /> | |
<xs:attribute name = ""publicationdate"" type=""xs:date"" use=""required"" /> | |
<xs:attribute name = ""ISBN"" type=""xs:string"" use=""required"" /> | |
</xs:complexType> | |
</xs:element> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:element> | |
</xs:schema>"; | |
private static string xmlFilename = "books.xml"; | |
private static string xsdFilename = "books.xsd"; | |
static void Main() | |
{ | |
try | |
{ | |
File.WriteAllText(xmlFilename, xml); | |
File.WriteAllText(xsdFilename, xsd); | |
XmlReaderSettings settings = new XmlReaderSettings(); | |
settings.Schemas.Add("http://www.contoso.com/books", xsdFilename); | |
settings.ValidationType = ValidationType.Schema; | |
XmlReader reader = XmlReader.Create(xmlFilename, settings); | |
XmlDocument document = new XmlDocument(); | |
document.Load(reader); | |
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler); | |
// the following call to Validate succeeds. | |
document.Validate(eventHandler); | |
// add a node so that the document is no longer valid | |
XPathNavigator navigator = document.CreateNavigator(); | |
navigator.MoveToFollowing("price", "http://www.contoso.com/books"); | |
XmlWriter writer = navigator.InsertAfter(); | |
writer.WriteStartElement("anotherNode", "http://www.contoso.com/books"); | |
writer.WriteEndElement(); | |
writer.Close(); | |
// the document will now fail to successfully validate | |
document.Validate(eventHandler); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
static void ValidationEventHandler(object sender, ValidationEventArgs e) | |
{ | |
switch (e.Severity) | |
{ | |
case XmlSeverityType.Error: | |
Console.WriteLine("Error: {0}", e.Message); | |
break; | |
case XmlSeverityType.Warning: | |
Console.WriteLine("Warning {0}", e.Message); | |
break; | |
} | |
} | |
} | |
} |
No comments:
Post a Comment