none
Deserialize and generated class RRS feed

  • Question

  • Hello,

    i have a generated class :

        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/", IsNullable=false)]
        public partial class GetSeuilEffectifResponse {
    ...
    }


    I have a Xml like :

    <?xml version=""1.0""?>
    <GetSeuilEffectifResponse xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.org/"">
    	<GetSeuilEffectifResult>
    		<ExtensionData />
    		<DateDebut>2016-02-22T11:48:49.21</DateDebut>
    		<DateFin xsi:nil=""true"" />
    		<IdSeuilControleDeclarationEffectif>1</IdSeuilControleDeclarationEffectif>
    		<IsActive>true</IsActive>
    		<MontantMini>15.00</MontantMini>
    		<PourcentageMaxi>3.00</PourcentageMaxi>
    		<PourcentageMini>-3.00</PourcentageMini>
    	</GetSeuilEffectifResult>
    </GetSeuilEffectifResponse>

    And i try to deserialize my Xml to an object

    XmlNodeReader reader = new XmlNodeReader(xmlDoc.DocumentElement);
    XmlSerializer serializer = new XmlSerializer(typeof(GetSeuilEffectifResponse));
    oTest = (GetSeuilEffectifResponse)serializer.Deserialize(reader);

    And it make me crazy. I've got this error :

    <GetSeuilEffectifRespons xmlns=''> was not expected

    How can i do ?


    Wednesday, April 13, 2016 3:24 PM

All replies

  • With some inferred details, it seems to work. Do you have a more complete code and a small published project that reproduces the issue?

    Wednesday, April 13, 2016 6:32 PM
  • Oups sorry, i've made a mistake.

    Bellow, the good xml message (without the xmlns=""http://tempuri.org/"")

    the missing namespace causes an error and I do not know how to adjust the thing

    <?xml version=""1.0""?>
    <GetSeuilEffectifResponse xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" >
    	<GetSeuilEffectifResult>
    		<ExtensionData />
    		<DateDebut>2016-02-22T11:48:49.21</DateDebut>
    		<DateFin xsi:nil=""true"" />
    		<IdSeuilControleDeclarationEffectif>1</IdSeuilControleDeclarationEffectif>
    		<IsActive>true</IsActive>
    		<MontantMini>15.00</MontantMini>
    		<PourcentageMaxi>3.00</PourcentageMaxi>
    		<PourcentageMini>-3.00</PourcentageMini>
    	</GetSeuilEffectifResult>
    </GetSeuilEffectifResponse>

    the missing namespace causes an error and I do not know how to adjust the thing

    • Edited by SandrAzure Thursday, April 14, 2016 8:11 AM
    Thursday, April 14, 2016 7:29 AM
  • If you cannot yet fix the XML, then try this workaround:

    XmlSerializer serializer = new XmlSerializer( typeof( GetSeuilEffectifResponse ), new XmlRootAttribute { Namespace = "", IsNullable = false } );
    

    • Marked as answer by SandrAzure Thursday, April 14, 2016 11:28 AM
    • Unmarked as answer by SandrAzure Thursday, April 14, 2016 1:40 PM
    Thursday, April 14, 2016 8:47 AM
  • Great! Thank You.
    Thursday, April 14, 2016 11:28 AM
  • Hop, it doesn't work.

    A null object have been created.

    Thursday, April 14, 2016 1:54 PM