Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I validate attribute names using XML Schema?
    primarykey
    data
    text
    <p>I'm trying to validate an XML file using an XSD schema, and I want to make certain that particular attributes are present using XmlReaderSettings and its ValidationEventHandler.</p> <p>Here's an example of what I'm trying to do. Each element has two attributes, "this" and "that". If one is missing or misspelled, I want to raise an exception. Currently, the ValidationEventHandler only raises an exception if the element name is invalid.</p> <p>XML file:</p> <pre><code>&lt;Addresses&gt; &lt;Address this="fu1" that="bar" /&gt; &lt;Address this="fu2" /&gt; &lt;Address thiss="fu3" that="bar" /&gt; &lt;/Addresses&gt; </code></pre> <p>XSD file:</p> <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1" ?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:attribute name="this" type="xs:string"/&gt; &lt;xs:attribute name="that" type="xs:string"/&gt; &lt;xs:element name="Address"&gt; &lt;xs:complexType&gt; &lt;xs:attribute ref="this" use="required"&gt;&lt;/xs:attribute&gt; &lt;xs:attribute ref="that" use="required"&gt;&lt;/xs:attribute&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="Addresses"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="Address" maxOccurs="unbounded"&gt; &lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; </code></pre> <p></p> <p>C# Code:</p> <pre><code>using System.Xml; using System.Xml.Schema; namespace test_validation { class Program { static void Main(string[] args) { string xsdpath = @"test.xsd"; string xmlpath = @"test.xml"; XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(null, xsdpath); settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += new ValidationEventHandler(settingsValidationEventHandler); XmlReader reader = XmlReader.Create(xmlpath, settings); while (reader.Read()) { } } static void settingsValidationEventHandler(object sender, ValidationEventArgs e) { Console.WriteLine(e.Message); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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