2

IDE: Microsoft Visual Studio 2010 Language : C#, XML

I am trying to deserialize XML file to object, but it throws me the error in XML document (5,4).Any suggestions ?

Here is the code I have tried ...

   public void ReadXMLtblFieldingDetails(string xmlFileXDetails)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(tblXDetails));
            TextReader reader = new StreamReader(xmlXDetails);
            object obj = serializer.Deserialize(reader);
            tblXDetails XmlData = (tblXDetails)obj;
            BusinessObjectManager.Add(XmlData); 

        }

And this is the XML document I want to deserialize ...

<?xml version="1.0"?>
<tblXDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <RowModified>0001-01-01T00:00:00</RowModified>
  <MachineID>6A7FEBFDCEFC4DSSDAXX</MachineID>      
  <fielder1_x_axis>0</fielder1_x_axis>
  <fielder1_y_axis>0</fielder1_y_axis>
  <fielder2_x_axis>0</fielder2_x_axis>
  <fielder2_y_axis>0</fielder2_y_axis>
  <fielder3_x_axis>0</fielder3_x_axis>
  <fielder3_y_axis>0</fielder3_y_axis>
  <fielder4_x_axis>0</fielder4_x_axis>
  <fielder4_y_axis>0</fielder4_y_axis>
  <fielder5_x_axis>0</fielder5_x_axis>
  <fielder5_y_axis>0</fielder5_y_axis>
  <fielder6_x_axis>0</fielder6_x_axis>
  <fielder6_y_axis>0</fielder6_y_axis>
  <fielder7_x_axis>0</fielder7_x_axis>
  <fielder7_y_axis>0</fielder7_y_axis>
  <fielder8_x_axis>0</fielder8_x_axis>
  <fielder8_y_axis>0</fielder8_y_axis>
  <fielder9_x_axis>0</fielder9_x_axis>
  <fielder9_y_axis>0</fielder9_y_axis>
</tblxDetails>

Here is the error : Error in XML Document(5,4) //Invalid Operation Exception was unhandled by user code

tblXDetails class:

public class tblXDetails
    {
        decimal _MachineID;
        float _fielder1_x_axis;
        float _fielder1_y_axis;
        float _fielder2_x_axis;
        float _fielder2_y_axis;
        float _fielder3_x_axis;
        float _fielder3_y_axis;
        float _fielder4_x_axis;
        float _fielder4_y_axis;
        float _fielder5_x_axis;
        float _fielder5_y_axis;
        float _fielder6_x_axis;
        float _fielder6_y_axis;
        float _fielder7_x_axis;
        float _fielder7_y_axis;
        float _fielder8_x_axis;
        float _fielder8_y_axis;
        float _fielder9_x_axis;
        float _fielder9_y_axis;
        public decimal MachineID
        {
            get { return _MachineID; }
            set { _MachineID = value; }
        }

        public float fielder1_x_axis
        {
            get { return _fielder1_x_axis; }
            set { _fielder1_x_axis = value; }
        }
        public float fielder1_y_axis
        {
            get { return _fielder1_y_axis; }
            set { _fielder1_y_axis = value; }
        }

        public float fielder2_x_axis
        {
            get { return _fielder2_x_axis; }
            set { _fielder2_x_axis = value; }
        }
        public float fielder2_y_axis
        {
            get { return _fielder2_y_axis; }
            set { _fielder2_y_axis = value; }
        }

        public float fielder3_x_axis
        {
            get { return _fielder3_x_axis; }
            set { _fielder3_x_axis = value; }
        }
        public float fielder3_y_axis
        {
            get { return _fielder3_y_axis; }
            set { _fielder3_y_axis = value; }
        }

        public float fielder4_x_axis
        {
            get { return _fielder4_x_axis; }
            set { _fielder4_x_axis = value; }
        }
        public float fielder4_y_axis
        {
            get { return _fielder4_y_axis; }
            set { _fielder4_y_axis = value; }
        }


        public float fielder5_x_axis
        {
            get { return _fielder5_x_axis; }
            set { _fielder5_x_axis = value; }
        }
        public float fielder5_y_axis
        {
            get { return _fielder5_y_axis; }
            set { _fielder5_y_axis = value; }
        }

        public float fielder6_x_axis
        {
            get { return _fielder6_x_axis; }
            set { _fielder6_x_axis = value; }
        }
        public float fielder6_y_axis
        {
            get { return _fielder6_y_axis; }
            set { _fielder6_y_axis = value; }
        }

        public float fielder7_x_axis
        {
            get { return _fielder7_x_axis; }
            set { _fielder7_x_axis = value; }
        }
        public float fielder7_y_axis
        {
            get { return _fielder7_y_axis; }
            set { _fielder7_y_axis = value; }
        }

        public float fielder8_x_axis
        {
            get { return _fielder8_x_axis; }
            set { _fielder8_x_axis = value; }
        }
        public float fielder8_y_axis
        {
            get { return _fielder8_y_axis; }
            set { _fielder8_y_axis = value; }
        }

        public float fielder9_x_axis
        {
            get { return _fielder9_x_axis; }
            set { _fielder9_x_axis = value; }
        }
        public float fielder9_y_axis
        {
            get { return _fielder9_y_axis; }
            set { _fielder9_y_axis = value; }
        }
Harsh Kumar Singhi
  • 201
  • 1
  • 3
  • 10
  • Try reading this link with same question: [here](http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document) – arielhad Jan 07 '15 at 06:22
  • It is hard to suggest anything if the only error you get in "(5,4)". Possibly complete error message will give a chance to someone to give you better advice than recommend to close as duplicate. – Alexei Levenkov Jan 07 '15 at 06:27
  • 1
    The start tag `` doesn't match the end tag ``. Note the case of the characters... – Fung Jan 07 '15 at 06:54
  • Sorry, but the case is correct... I tried changing the case – Harsh Kumar Singhi Jan 07 '15 at 07:00
  • @HarshKumarSinghi You should check the InnerException, which would gives you & the others more details. `Error in XML Document(5,4)` only indicates that something went wrong near the first `` tag – Fung Jan 07 '15 at 07:15
  • Show us the 'tblXDetails' class. I bet some values are incompatible with the `tblXDetails` underlying types. – t3chb0t Jan 07 '15 at 07:23
  • Please have a look at the tblXDetails class – Harsh Kumar Singhi Jan 07 '15 at 07:38

1 Answers1

2

The MachineID must be a decimal (according to your class definition) but in the xml an incompatible value is provided: 6A7FEBFDCEFC4DSSDAXX. If you set it to 1 in the xml the serialization passes. I'm afraid you'll need to write a custom (de)serializer or change the type of the MachineID if possible.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118