Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following <a href="https://msdn.microsoft.com/en-us/library/db5d30c9(v=vs.110).aspx" rel="noreferrer">example</a> validates an XML file and generates the appropriate error or warning.</p> <pre><code>using System; using System.IO; using System.Xml; using System.Xml.Schema; public class Sample { public static void Main() { //Load the XmlSchemaSet. XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.Add("urn:bookstore-schema", "books.xsd"); //Validate the file using the schema stored in the schema set. //Any elements belonging to the namespace "urn:cd-schema" generate //a warning because there is no schema matching that namespace. Validate("store.xml", schemaSet); Console.ReadLine(); } private static void Validate(String filename, XmlSchemaSet schemaSet) { Console.WriteLine(); Console.WriteLine("\r\nValidating XML file {0}...", filename.ToString()); XmlSchema compiledSchema = null; foreach (XmlSchema schema in schemaSet.Schemas()) { compiledSchema = schema; } XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(compiledSchema); settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); settings.ValidationType = ValidationType.Schema; //Create the schema validating reader. XmlReader vreader = XmlReader.Create(filename, settings); while (vreader.Read()) { } //Close the reader. vreader.Close(); } //Display any warnings or errors. private static void ValidationCallBack(object sender, ValidationEventArgs args) { if (args.Severity == XmlSeverityType.Warning) Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message); else Console.WriteLine("\tValidation error: " + args.Message); } } </code></pre> <p>The preceding example uses the following input files.</p> <pre><code>&lt;?xml version='1.0'?&gt; &lt;bookstore xmlns="urn:bookstore-schema" xmlns:cd="urn:cd-schema"&gt; &lt;book genre="novel"&gt; &lt;title&gt;The Confidence Man&lt;/title&gt; &lt;price&gt;11.99&lt;/price&gt; &lt;/book&gt; &lt;cd:cd&gt; &lt;title&gt;Americana&lt;/title&gt; &lt;cd:artist&gt;Offspring&lt;/cd:artist&gt; &lt;price&gt;16.95&lt;/price&gt; &lt;/cd:cd&gt; &lt;/bookstore&gt; </code></pre> <p>books.xsd</p> <pre><code>&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:bookstore-schema" elementFormDefault="qualified" targetNamespace="urn:bookstore-schema"&gt; &lt;xsd:element name="bookstore" type="bookstoreType"/&gt; &lt;xsd:complexType name="bookstoreType"&gt; &lt;xsd:sequence maxOccurs="unbounded"&gt; &lt;xsd:element name="book" type="bookType"/&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="bookType"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="title" type="xsd:string"/&gt; &lt;xsd:element name="author" type="authorName"/&gt; &lt;xsd:element name="price" type="xsd:decimal"/&gt; &lt;/xsd:sequence&gt; &lt;xsd:attribute name="genre" type="xsd:string"/&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="authorName"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="first-name" type="xsd:string"/&gt; &lt;xsd:element name="last-name" type="xsd:string"/&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;/xsd:schema&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload