none
Problem with XmlSerializer - "There was an error reflecting type.." RRS feed

  • Question

  • I'm working .NetFramework4 Client Profile / Visual Studio 2017 Community/ Windows8.

    I want to read some XML files based on XSD schema (BDOT500.xsd).

    I've automatically generated runtime classes from XSD schema file using by 'XML Schema Definition Tool(Xsd.exe)'.

    And programmmed this code:

                try
                    {
                    XmlSerializer serializer = new XmlSerializer(typeof(BDZ_ObiektBDOT500PropertyType));
    
                    List<BDZ_ObiektBDOT500PropertyType> myObjects = new List<BDZ_ObiektBDOT500PropertyType>();
                    XmlReader reader = XmlReader.Create("bdot500.xml");
                    myObjects = (List<BDZ_ObiektBDOT500PropertyType>)serializer.Deserialize(reader);
                    reader.Close();
                    }
                catch (Exception ex)
                    {
                    DumpException(ex);
                    }

    I've got an an exception.

    This is my sample XML file

    <?xml version="1.0" encoding="UTF-8"?>
    <gml:FeatureCollection gml:id="obiekty" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:bt="urn:gugik:specyfikacje:gmlas:modelPodstawowy:1.0" xmlns:bdz="urn:gugik:specyfikacje:gmlas:bazaDanychObiektowTopograficznych500:1.0" xsi:schemaLocation="urn:gugik:specyfikacje:gmlas:bazaDanychObiektowTopograficznych500:1.0 BDOT500.xsd">
    
    <gml:featureMember>
    <bdz:BDZ_PunktOOkreslonejWysokosci gml:id="PL.PZGiK.0000.BDOT500_6850F355-DA9E-47B0-8A5F-D86B3DE58890_2017-12-13T12-56-50">
    <gml:identifier codeSpace="http://iip.gov.pl">urn:pzgik:id:PL.PZGiK.0000.BDOT500:6850F355-DA9E-47B0-8A5F-D86B3DE58890:2017-12-13T12:56:50</gml:identifier>
    <bdz:idIIP>
    <bt:BT_Identyfikator>
    <bt:lokalnyId>6850F355-DA9E-47B0-8A5F-D86B3DE58890</bt:lokalnyId>
    <bt:przestrzenNazw>PL.PZGiK.0000.BDOT500</bt:przestrzenNazw>
    <bt:wersjaId>2017-12-13T12:56:50</bt:wersjaId>
    </bt:BT_Identyfikator>
    </bdz:idIIP>
    <bdz:startObiekt>2017-12-13</bdz:startObiekt>
    <bdz:cyklZyciaObiektu>
    <bt:BT_CyklZyciaInfo>
    <bt:poczatekWersjiObiektu>2017-12-13T12:56:50</bt:poczatekWersjiObiektu>
    </bt:BT_CyklZyciaInfo>
    </bdz:cyklZyciaObiektu>
    <bdz:zrodlo>pomiarNaOsnowe</bdz:zrodlo>
    <bdz:idMaterialu>
    <bt:BT_IdMaterialu>
    <bt:pierwszyCzlon>P</bt:pierwszyCzlon>
    <bt:drugiCzlon>1812</bt:drugiCzlon>
    <bt:trzeciCzlon>2016</bt:trzeciCzlon>
    <bt:czwartyCzlon>1443</bt:czwartyCzlon>
    </bt:BT_IdMaterialu>
    </bdz:idMaterialu>
    <bdz:dataPomiaru>2016-09-22</bdz:dataPomiaru>
    <bdz:geometria>
    <gml:Point gml:id="GBDZ_1_382_1" srsName="urn:ogc:def:crs:EPSG::2178" srsDimension="2">
    <gml:pos>5.59679553000000E+0006 7.58557448000000E+0006</gml:pos>
    </gml:Point>
    </bdz:geometria>
    <bdz:rodzajPunktu>punktWysokosciowyNaturalny</bdz:rodzajPunktu>
    <bdz:rzednaGory>158.8</bdz:rzednaGory>
    </bdz:BDZ_PunktOOkreslonejWysokosci>
    </gml:featureMember>
    
    </gml:FeatureCollection>
    

    1. XSD schema (BDOT500.xsd):

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:bdz="urn:gugik:specyfikacje:gmlas:bazaDanychObiektowTopograficznych500:1.0" xmlns:bt="urn:gugik:specyfikacje:gmlas:modelPodstawowy:1.0" targetNamespace="urn:gugik:specyfikacje:gmlas:bazaDanychObiektowTopograficznych500:1.0" elementFormDefault="qualified" version="2.0">
    	<import namespace="http://www.opengis.net/gml/3.2" schemaLocation="gml.xsd"/>
    	<import namespace="urn:gugik:specyfikacje:gmlas:modelPodstawowy:1.0" schemaLocation="BT_ModelPodstawowy.xsd"/>
    	<include schemaLocation="slownikiBDOT500.xsd"/>
    	<!-- ============================================================= -->
    	<element name="BDZ_ObiektBDOT500" type="bdz:BDZ_ObiektBDOT500Type" abstract="true" substitutionGroup="gml:AbstractFeature"/>
    	<complexType name="BDZ_ObiektBDOT500Type" abstract="true">
    		<complexContent>
    			<extension base="gml:AbstractFeatureType">
    				<sequence>
    					<element name="idIIP" type="bt:BT_IdentyfikatorPropertyType"/>
    					<element name="startObiekt" type="date"/>
    					<element name="cyklZyciaObiektu" type="bt:BT_CyklZyciaInfoPropertyType"/>
    					<element name="koniecObiekt" type="date" minOccurs="0"/>
    					<element name="zrodlo" type="bdz:BDZ_ZrodloType"/>
    					<element name="idMaterialu" type="bt:BT_IdMaterialuPropertyType" minOccurs="0" maxOccurs="unbounded"/>
    					<element name="dataPomiaru" nillable="true" minOccurs="0">
    						<complexType>
    							<simpleContent>
    								<extension base="date">
    									<attribute name="nilReason" type="gml:NilReasonType"/>
    								</extension>
    							</simpleContent>
    						</complexType>
    					</element>
    					<element name="dokument" type="string" minOccurs="0" maxOccurs="unbounded"/>
    					<element name="informacja" type="string" minOccurs="0" maxOccurs="unbounded"/>
    				</sequence>
    			</extension>
    		</complexContent>
    	</complexType>
    	<complexType name="BDZ_ObiektBDOT500PropertyType">
    		<sequence minOccurs="0">
    			<element ref="bdz:BDZ_ObiektBDOT500"/>
    		</sequence>
    		<attributeGroup ref="gml:AssociationAttributeGroup"/>
    		<attributeGroup ref="gml:OwnershipAttributeGroup"/>
    	</complexType>
    
    	<element name="BDZ_PunktOOkreslonejWysokosci" type="bdz:BDZ_PunktOOkreslonejWysokosciType" substitutionGroup="bdz:BDZ_ObiektBDOT500"/>
    	<complexType name="BDZ_PunktOOkreslonejWysokosciType">
    		<complexContent>
    			<extension base="bdz:BDZ_ObiektBDOT500Type">
    				<sequence>
    					<element name="rodzajPunktu" type="bdz:BDZ_RodzajPunktuType"/>
    					<element name="rzednaGory" type="double" minOccurs="0"/>
    					<element name="rzednaDolu" type="double" minOccurs="0"/>
    				</sequence>
    			</extension>
    		</complexContent>
    	</complexType>
    	<complexType name="BDZ_PunktOOkreslonejWysokosciPropertyType">
    		<sequence minOccurs="0">
    			<element ref="bdz:BDZ_PunktOOkreslonejWysokosci"/>
    		</sequence>
    		<attributeGroup ref="gml:AssociationAttributeGroup"/>
    		<attributeGroup ref="gml:OwnershipAttributeGroup"/>
    	</complexType>
    
    </schema>

    2. XSD schema (BT_ModelPodstawowy.xsd):

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:bt="urn:gugik:specyfikacje:gmlas:modelPodstawowy:1.0" targetNamespace="urn:gugik:specyfikacje:gmlas:modelPodstawowy:1.0" elementFormDefault="qualified" attributeFormDefault="unqualified" version="3.6">
    	<import namespace="http://www.opengis.net/gml/3.2" schemaLocation="http://schemas.opengis.net/gml/3.2.1/gml.xsd"/>
    	<import namespace="http://www.isotc211.org/2005/gmd" schemaLocation="http://schemas.opengis.net/iso/19139/20070417/gmd/gmd.xsd"/>
    	<!--===========Typy podstawowe============-->
    	<!--======================================-->
    	<element name="BT_Identyfikator" type="bt:BT_IdentyfikatorType" substitutionGroup="gml:AbstractObject"/>
    	<complexType name="BT_IdentyfikatorType">
    		<sequence>
    			<element name="lokalnyId">
    				<simpleType>
    					<restriction base="string">
    						<pattern value="[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}"/>
    					</restriction>
    				</simpleType>
    			</element>
    			<element name="przestrzenNazw">
    				<simpleType>
    					<restriction base="string">
    						<pattern value="PL\.[A-Za-z]{1,6}\.\d{1,6}\.[A-Za-z0-9]{1,8}"/>
    					</restriction>
    				</simpleType>
    			</element>
    			<element name="wersjaId" type="dateTime" minOccurs="0"/>
    		</sequence>
    	</complexType>
    	<complexType name="BT_IdentyfikatorPropertyType">
    		<sequence>
    			<element ref="bt:BT_Identyfikator"/>
    		</sequence>
    		<attributeGroup ref="gml:OwnershipAttributeGroup"/>
    	</complexType>
    	<!--======================================-->
    	<element name="BT_CyklZyciaInfo" type="bt:BT_CyklZyciaInfoType" substitutionGroup="gml:AbstractObject"/>
    	<complexType name="BT_CyklZyciaInfoType">
    		<sequence>
    			<element name="poczatekWersjiObiektu" type="dateTime"/>
    			<element name="koniecWersjiObiektu" type="dateTime" minOccurs="0"/>
    		</sequence>
    	</complexType>
    	<complexType name="BT_CyklZyciaInfoPropertyType">
    		<sequence>
    			<element ref="bt:BT_CyklZyciaInfo"/>
    		</sequence>
    		<attributeGroup ref="gml:OwnershipAttributeGroup"/>
    	</complexType>
    	<!--======================================-->
    	<element name="BT_IdMaterialu" type="bt:BT_IdMaterialuType" substitutionGroup="gml:AbstractObject"/>
    	<complexType name="BT_IdMaterialuType">
    		<sequence>
    			<element name="pierwszyCzlon" type="bt:BT_OznaczenieZasobuType"/>
    			<element name="drugiCzlon" type="string"/>
    			<element name="trzeciCzlon" type="positiveInteger"/>
    			<element name="czwartyCzlon" type="nonNegativeInteger"/>
    		</sequence>
    	</complexType>
    	<complexType name="BT_IdMaterialuPropertyType">
    		<sequence>
    			<element ref="bt:BT_IdMaterialu"/>
    		</sequence>
    		<attributeGroup ref="gml:OwnershipAttributeGroup"/>
    	</complexType>
    	<!--======================================-->
    	<simpleType name="BT_OznaczenieZasobuType">
    		<restriction base="string">
    			<enumeration value="C">
    				<annotation>
    					<appinfo>
    						<gml:description>centralny</gml:description>
    					</appinfo>
    				</annotation>
    			</enumeration>
    			<enumeration value="W">
    				<annotation>
    					<appinfo>
    						<gml:description>wojewodzki</gml:description>
    					</appinfo>
    				</annotation>
    			</enumeration>
    			<enumeration value="P">
    				<annotation>
    					<appinfo>
    						<gml:description>powiatowy</gml:description>
    					</appinfo>
    				</annotation>
    			</enumeration>
    		</restriction>
    	</simpleType>
    </schema>
    

    3. XSD schema (slownikiBDOT500.xsd):

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:bdz="urn:gugik:specyfikacje:gmlas:bazaDanychObiektowTopograficznych500:1.0" targetNamespace="urn:gugik:specyfikacje:gmlas:bazaDanychObiektowTopograficznych500:1.0" elementFormDefault="qualified" version="2.0">
    <!-- ============================================================= -->
    	<simpleType name="BDZ_ZrodloType">
    		<restriction base="string">
    			<enumeration value="pomiarNaOsnowe"/>
    			<enumeration value="digitalizacjaIWektoryzacja"/>
    			<enumeration value="fotogrametria"/>
    			<enumeration value="pomiarWOparciuOElementyMapy"/>
    			<enumeration value="inne"/>
    			<enumeration value="nieokreslone"/>
    			<enumeration value="niepoprawne"/>
    		</restriction>
    	</simpleType>
    
    	<simpleType name="BDZ_RodzajPunktuType">
    		<restriction base="string">
    			<enumeration value="punktWysokosciowyNaturalny"/>
    			<enumeration value="punktWysokosciowySztuczny"/>
    		</restriction>
    	</simpleType>
    
    </schema>




    Friday, May 11, 2018 12:45 PM

All replies

  • The XSD's you posted seem to have a lot of validation warnings making the resulting code questionable at best. Please post the actual generated classes for the schema that you're having issues with.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, May 11, 2018 3:22 PM
    Moderator
  • Hi Wizardek,

    Thank you for posting here.

    >>I want to read some XML files based on XSD schema (BDOT500.xsd).

    For your question, I am not sure what you want to do with serialization. If you want to read xml files based on XSD schema, you could refer to the MSDN document.

    The following code example illustrates reading and writing XML schemas from and to a file. The code example takes the example.xsd file, reads it into an XmlSchema object using the staticRead method, and then writes the file to the console and a new new.xsd file

    https://docs.microsoft.com/en-us/dotnet/standard/data/xml/reading-and-writing-xml-schemas

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 14, 2018 7:28 AM
    Moderator
  • Hi Wendy

    Thank's for Your reply on my post.

    Yes - I want to read XML file based on XSD schemat (controlling data types, data limitations, data dictionaries, etc.) but my XML files I want to read may be large up to 300 MB.

    I'm newbie to C# especially to XML reading, so I'tried to google internet and found information pointing to deserialization.

    So maybe it's the wrong way on reading XML file.

    That's why can You give the advise (maybe sample code based on delivered XML & XSD files) how to read properly XML file based on XSD schemat into own user structures/classes.

    Looking forward on Your answer.

    Best Regards,

    Wizardek

    ps.
    I can not post actual generated classes for the schema cause they are about 26 000 lines of code.
    Monday, May 14, 2018 10:45 AM