c# - Xml serialization with custom schema and IXmlSerializable -


i'm trying serialize object using ixmlserializable , xmlschemaproviderattribute.

the schema looks this:

<?xml version="1.0" encoding="utf-8"?> <xs:schema id="buildingsschema" targetnamespace="buildingmodelschema"     elementformdefault="qualified"     xmlns="buildingmodelschema"     xmlns:xs="http://www.w3.org/2001/xmlschema" >     <xs:element name="buildings">         <xs:complextype>             <xs:sequence>                 <xs:element name="building" minoccurs="0" maxoccurs="unbounded">                     <xs:complextype>                         <xs:sequence>                         ... 

and code schema looks (as found here):

[<literal>] let xml_namespace                       = "buildingmodelschema" [<literal>] let xsd_schema_path                     = @"buildingsschema.xsd"  [<xmlschemaprovider(buildingsschema)>] type buildingsstatistics() =     interface ixmlserializable     ...     static member buildingsschema(xs: xmlschemaset) =         let serializer = new xmlserializer(typeof<xmlschema>)         let schema = (serializer.deserialize(new xmltextreader(xsd_schema_path), null)) :?> xmlschema         xs.xmlresolver <- new xmlurlresolver()         xs.add(schema) |> ignore          new xmlqualifiedname("buildings", xml_namespace)     ... 

now when try serialize object xmlserializer exception: {"planermodel.buildingsstatistics.buildingsschema() must return valid type name. type 'buildings' cannot found in targetnamespace='buildingmodelschema'."}

edit:
after trying deserialize valid test document same error message. expect there wrong xml schema due fact first time work xml schemata don't seem find mistake.

further testing shows exception thrown on initialization of xmlserializer:

xmlserializer ser = new xmlserializer(typeof(buildingsstatistics)); // exception thrown here ser.deserialize(...)                                                // not executed 

furthermore xmlschema added in buildingsschema method not contain element (in case 0 elements , 14 items 'empty' (no line number, position, etc.)).

i found solution problem: had change schema definition to

<xs:complextype name="buildingstype">     <xs:sequence>         <xs:element name="building" type="buildingtype" minoccurs="1" maxoccurs="unbounded"/>     </xs:sequence> </xs:complextype> ... 

and returned xmlqualifiedname inside buildingsschema to

new xmlqualifiedname("buildingtype", xml_namespace)  

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -