Note that there are some explanatory texts on larger screens.

plurals
  1. POc# XML Schema validation
    primarykey
    data
    text
    <p>I have a nice XML file like this:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Assets Path="C:\Users\r3plica\Google Drive"&gt; &lt;Assetd&gt; &lt;FileName&gt;Boomerang - Error codes.xlsx&lt;/FileName&gt; &lt;DisplayName&gt;Boomerang - Error codes&lt;/DisplayName&gt; &lt;Description&gt;This is the Boomerang error codes file&lt;/Description&gt; &lt;Tags&gt; &lt;Tag&gt;Excel&lt;/Tag&gt; &lt;Tag&gt;Boomerang&lt;/Tag&gt; &lt;/Tags&gt; &lt;Categories&gt; &lt;Category&gt;1&lt;/Category&gt; &lt;Category&gt;4&lt;/Category&gt; &lt;/Categories&gt; &lt;/Assetd&gt; &lt;Asset&gt; &lt;FileName&gt;Issue Tracker v5.xlsx&lt;/FileName&gt; &lt;Description&gt;This is the issue tracker for Skipstone&lt;/Description&gt; &lt;Tags&gt; &lt;Tag&gt;Excel&lt;/Tag&gt; &lt;Tag&gt;Skipstone&lt;/Tag&gt; &lt;/Tags&gt; &lt;Categories&gt; &lt;Category&gt;1&lt;/Category&gt; &lt;Category&gt;4&lt;/Category&gt; &lt;/Categories&gt; &lt;/Asset&gt; &lt;/Assets&gt; </code></pre> <p>and then I have my schema which I created like this:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xs:schema id="data" targetNamespace="http://tempuri.org/data.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/data.xsd" xmlns:mstns="http://tempuri.org/data.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" &gt; &lt;xs:element name="Assets"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="Asset" type="Asset" minOccurs="1" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:complexType name="Asset"&gt; &lt;xs:sequence&gt; &lt;xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1" /&gt; &lt;xs:element name="DisplayName" type="xs:string" minOccurs="0" maxOccurs="1" /&gt; &lt;xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="1" /&gt; &lt;xs:element name="Tags" type="Tags" minOccurs="0" maxOccurs="1" /&gt; &lt;xs:element name="Categories" type="Categories" minOccurs="1" maxOccurs="1" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Tags"&gt; &lt;xs:sequence&gt; &lt;xs:element name="Tag" type="xs:string" minOccurs="1" maxOccurs="unbounded" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Categories"&gt; &lt;xs:sequence&gt; &lt;xs:element name="Category" type="xs:int" minOccurs="1" maxOccurs="unbounded" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>As far as I can see looking at that, the xml file is invalid because the first element is Assetd and not Asset, but if I run my c# code:</p> <pre><code>XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add("http://tempuri.org/data.xsd", "data.xsd"); XDocument doc = XDocument.Load(openFileDialog1.FileName); string msg = ""; doc.Validate(schemas, (o, err) =&gt; { msg = err.Message; }); Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg); </code></pre> <p>it tells me the xml is valid... If I use this code:</p> <pre><code>// Set the validation settings. XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add("http://tempuri.org/data.xsd", "data.xsd"); settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); // Create the XmlReader object. XmlReader reader = XmlReader.Create(openFileDialog1.FileName, settings); // Parse the file. while (reader.Read()) ; </code></pre> <p>I get this output in the console:</p> <pre><code>Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Assets'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the attribute 'Path'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Assetd'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'FileName'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'DisplayName'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Description'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tags'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Categories'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Asset'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'FileName'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Description'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tags'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Categories'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. </code></pre> <p>Can anyone tell me what I am doing wrong please? It is killing me :(</p> <p>cheers, /r3plica</p>
    singulars
    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.
 

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