c# XML Schema validation -


i have nice xml file this:

    <?xml version="1.0" encoding="utf-8" ?>     <assets path="c:\users\r3plica\google drive">         <assetd>             <filename>boomerang - error codes.xlsx</filename>             <displayname>boomerang - error codes</displayname>             <description>this boomerang error codes file</description>             <tags>                 <tag>excel</tag>                 <tag>boomerang</tag>             </tags>             <categories>                 <category>1</category>                 <category>4</category>             </categories>         </assetd>         <asset>             <filename>issue tracker v5.xlsx</filename>             <description>this issue tracker skipstone</description>             <tags>                 <tag>excel</tag>                 <tag>skipstone</tag>             </tags>             <categories>                 <category>1</category>                 <category>4</category>             </categories>         </asset>     </assets> 

and have schema created this:

    <?xml version="1.0" encoding="utf-8"?>     <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"     >       <xs:element name="assets">         <xs:complextype>           <xs:sequence>             <xs:element name="asset" type="asset" minoccurs="1" />           </xs:sequence>         </xs:complextype>       </xs:element>        <xs:complextype name="asset">         <xs:sequence>           <xs:element name="filename" type="xs:string" minoccurs="1" maxoccurs="1" />           <xs:element name="displayname" type="xs:string" minoccurs="0" maxoccurs="1" />           <xs:element name="description" type="xs:string" minoccurs="0" maxoccurs="1" />           <xs:element name="tags" type="tags" minoccurs="0" maxoccurs="1" />           <xs:element name="categories" type="categories" minoccurs="1" maxoccurs="1" />         </xs:sequence>       </xs:complextype>        <xs:complextype name="tags">         <xs:sequence>           <xs:element name="tag" type="xs:string" minoccurs="1" maxoccurs="unbounded" />         </xs:sequence>       </xs:complextype>        <xs:complextype name="categories">         <xs:sequence>           <xs:element name="category" type="xs:int" minoccurs="1" maxoccurs="unbounded" />         </xs:sequence>       </xs:complextype>     </xs:schema> 

as far can see looking @ that, xml file invalid because first element assetd , not asset, if run c# 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) => {     msg = err.message; }); console.writeline(msg == "" ? "document valid" : "document invalid: " + msg); 

it tells me xml valid... if use code:

// set 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 xmlreader object. xmlreader reader = xmlreader.create(openfiledialog1.filename, settings);  // parse file.  while (reader.read()) ; 

i output in console:

warning: matching schema not found.  no validation occurred. not find schema information element 'assets'. warning: matching schema not found.  no validation occurred. not find schema information attribute 'path'. warning: matching schema not found.  no validation occurred. not find schema information element 'assetd'. warning: matching schema not found.  no validation occurred. not find schema information element 'filename'. warning: matching schema not found.  no validation occurred. not find schema information element 'displayname'. warning: matching schema not found.  no validation occurred. not find schema information element 'description'. warning: matching schema not found.  no validation occurred. not find schema information element 'tags'. warning: matching schema not found.  no validation occurred. not find schema information element 'tag'. warning: matching schema not found.  no validation occurred. not find schema information element 'tag'. warning: matching schema not found.  no validation occurred. not find schema information element 'categories'. warning: matching schema not found.  no validation occurred. not find schema information element 'category'. warning: matching schema not found.  no validation occurred. not find schema information element 'category'. warning: matching schema not found.  no validation occurred. not find schema information element 'asset'. warning: matching schema not found.  no validation occurred. not find schema information element 'filename'. warning: matching schema not found.  no validation occurred. not find schema information element 'description'. warning: matching schema not found.  no validation occurred. not find schema information element 'tags'. warning: matching schema not found.  no validation occurred. not find schema information element 'tag'. warning: matching schema not found.  no validation occurred. not find schema information element 'tag'. warning: matching schema not found.  no validation occurred. not find schema information element 'categories'. warning: matching schema not found.  no validation occurred. not find schema information element 'category'. warning: matching schema not found.  no validation occurred. not find schema information element 'category'. 

can tell me doing wrong please? killing me :(

cheers, /r3plica

you need set default namespace in xml, this:

<?xml version="1.0" encoding="utf-8"  ?>     <assets xmlns="http://tempuri.org/data.xsd">         <asset>             <filename>boomerang - error codes.xlsx</filename>             <displayname>boomerang - error codes</displayname>             <description>this boomerang error codes file</description>             <tags>                 <tag>excel</tag>                 <tag>boomerang</tag>             </tags>             <categories>                 <category>1</category>                 <category>4</category>             </categories>         </asset>         <asset>             <filename>issue tracker v5.xlsx</filename>             <description>this issue tracker skipstone</description>             <tags>                 <tag>excel</tag>                 <tag>skipstone</tag>             </tags>             <categories>                 <category>1</category>                 <category>4</category>             </categories>         </asset>     </assets> 

also, there number of other problems:

path attribute not defined in schema, 'assetd' element not defined. maxoccurs="unbounded" need set in schema xs:element name="asset"

in case if cannot modify xml, need remove target schema xsd:

<xs:schema id="data"     xmlns:mstns="http://tempuri.org/data.xsd"     xmlns:xs="http://www.w3.org/2001/xmlschema" > 

and register schema this:

settings.schemas.add(null, "data.xsd"); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -