Getting error when serialize xml to c# class -


i'm trying serialize xml file c# object i'm getting following error:

there error in xml document (1, 64).

i copy xml file , pasted special xml in visual studio.

this xml:

<?xml version="1.0" encoding="windows-1252" standalone="yes"?><root><record codunic="g15_455262_ro6739810_2016" codstatie="g15" docid="1" tipdoc="gastro" nrdoc="455262" datadoc="2016-01-21" dataintr="2016-01-21" nrintr="0" retur="0" dataanulare="" codfurnizor="ro6739810" denfurnizor="sc ifantis ggg srl" valoarefaratva="171.91" valoaretva="15.47" /></root> 

this generated "special class" :

using system; using system.collections.generic; using system.linq; using system.text;  namespace xmlchecktool.clase {  /// <remarks/> [system.serializableattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(anonymoustype = true)] [system.xml.serialization.xmlrootattribute(namespace = "", isnullable = false)] public partial class intrcap {      private rootrecord recordfield;      /// <remarks/>     public rootrecord record     {                 {             return this.recordfield;         }         set         {             this.recordfield = value;         }     } }  /// <remarks/> [system.serializableattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(anonymoustype = true)] public partial class rootrecord {      private string codunicfield;      private string codstatiefield;      private int docidfield;      private string tipdocfield;      private uint nrdocfield;      private system.datetime datadocfield;      private system.datetime dataintrfield;      private int nrintrfield;      private int returfield;      private string dataanularefield;      private string codfurnizorfield;      private string denfurnizorfield;      private decimal valoarefaratvafield;      private decimal valoaretvafield;      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public string codunic     {                 {             return this.codunicfield;         }         set         {             this.codunicfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public string codstatie     {                 {             return this.codstatiefield;         }         set         {             this.codstatiefield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public int docid     {                 {             return this.docidfield;         }         set         {             this.docidfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public string tipdoc     {                 {             return this.tipdocfield;         }         set         {             this.tipdocfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public uint nrdoc     {                 {             return this.nrdocfield;         }         set         {             this.nrdocfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute(datatype = "date")]     public system.datetime datadoc     {                 {             return this.datadocfield;         }         set         {             this.datadocfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute(datatype = "date")]     public system.datetime dataintr     {                 {             return this.dataintrfield;         }         set         {             this.dataintrfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public int nrintr     {                 {             return this.nrintrfield;         }         set         {             this.nrintrfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public int retur     {                 {             return this.returfield;         }         set         {             this.returfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public string dataanulare     {                 {             return this.dataanularefield;         }         set         {             this.dataanularefield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public string codfurnizor     {                 {             return this.codfurnizorfield;         }         set         {             this.codfurnizorfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public string denfurnizor     {                 {             return this.denfurnizorfield;         }         set         {             this.denfurnizorfield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public decimal valoarefaratva     {                 {             return this.valoarefaratvafield;         }         set         {             this.valoarefaratvafield = value;         }     }      /// <remarks/>     [system.xml.serialization.xmlattributeattribute()]     public decimal valoaretva     {                 {             return this.valoaretvafield;         }         set         {             this.valoaretvafield = value;         }     } }   } 

and method xml object:

 public static void filecapintr(string xmlfile) {         xmlserializer serializer = new xmlserializer(typeof(intrcap));         intrcap i;         using (stream reader = new filestream(xmlfile, filemode.open))         {             // call deserialize method restore object's state.             = (intrcap)serializer.deserialize(reader);         }       } 

any please? lot!

your error

there error in xml document (1, 64).

has innerexception reads:

<root xmlns=''> not expected.

simple fix: specify elementname of root:

[system.xml.serialization.xmlrootattribute(     namespace = "",      isnullable = false,      elementname = "root"  )] 

or specify when creating serializer

xmlserializer serializer = new xmlserializer(     typeof(intrcap),      null,      null,      new xmlrootattribute("root"),     "" ); 

proof dotnetfiddle


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -