views:

48

answers:

2

This is a sample of the XML file I am attempting to deserialize

<TradeFills>
 <TradeFill>
  <Broker>xxx</Broker>
  <CustomerAccount/>
  <qwFillTransID>xxxxxxxxxxxxxxxxxxx</qwFillTransID>
  <qwPrevTransID>xxx</qwPrevTransID>
  <qwGroupTransID>xxxxxxxxxxxxxxxxx</qwGroupTransID>
  <GroupTransID>xxxxxxxx</GroupTransID>
  <TransID>x</TransID>
  <Service>xxxxxxxxxxxxxxxx</Service>
  <Symbol>xx</Symbol>
  <Exchange>xxxxx</Exchange>
  <InstClass>xxxxxxxx</InstClass>
  <InstSubClass>xxxxxxx</InstSubClass>
  <ContractSymbol>xxxx</ContractSymbol>
  <ExpirationDate>xxxxxxxx</ExpirationDate>
  <Month>xx</Month>
  <Year>xxxx</Year>
  <Strike>xxx</Strike>
  <TradePCU>xxxx</TradePCU>
  <Buy>x</Buy>
  <Quantity>xx</Quantity>
  <Price>xxxxx</Price>
  <FillTime>xxxxxxxxxxxxxxx</FillTime>
  <PosUpdated>xxxxxxxxxxx</PosUpdated>
  <Description/>
 </TradeFill>
</TradeFills>

This is the code I am using:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace DeserializeXML
{
    public class Program
    {

        // This is the class that will be deserialized.
        [Serializable()]
        public class TradeFill
        {
            [XmlElement("Broker")]
            public string broker;

            [XmlElement("qwFillTransID")]
            public string qwFillTransId;

            [XmlElement("qwPrevTransID")]
            public string qwPrevTransId;

            [XmlElement("qwGroupTransID")]
            public string qwGroupTransId;

            [XmlElement("GroupTransID")]
            public string GroupTransID;

            [XmlElement("TransID")]
            public string TransId;

            [XmlElement("Service")]
            public string Service;

            [XmlElement("Exchange")]
            public string Exchange;

            [XmlElement("InstClass")]
            public string InstClass;

            [XmlElement("InstSubClass")]
            public string InstSubClass;

            [XmlElement("ContractSymbol")]
            public string ConSymbol;

            [XmlElement("ExpirationDate")]
            public DateTime ExpDate;

            [XmlElement("Month")]
            public int month;

            [XmlElement("Year")]
            public int year;

            [XmlElement("Strike")]
            public double strike;

            [XmlElement("TradePCU")]
            public string TradePCU;

            [XmlElement("Buy")]
            public int buy;

            [XmlElement("Quantity")]
            public int quantity;

            [XmlElement("Price")]
            public double price;

            [XmlElement("FillTime")]
            public DateTime FillTime;

            [XmlElement("PosUpdated")]
            public string PosUpdated;

        }


        [XmlRootAttribute("TradeFills")]
        public class SIGTrades
        {
            [XmlElement("TradeFills")]
            public TradeFill[] TradeFills{ get; set; }
        }


        [Serializable()]
        public class Test
        {
             public static void Main()
            {
                Test t = new Test();
              // Read a purchase order.
                t.DeserializeObject("c:\\testtrades.xml");
            }

             private void DeserializeObject(string filename)
             {
                 Console.WriteLine("Reading with Stream");
                 // Create an instance of the XmlSerializer.
                 XmlSerializer serializer =
                 new XmlSerializer(typeof(TradeFill));
                 // Reading the XML document requires a FileStream.
                 Stream reader = new FileStream(filename, FileMode.Open);

                 // Declare an object variable of the type to be deserialized.
                 TradeFill i;

                 // Call the Deserialize method to restore the object's state.
                 i = (TradeFill)serializer.Deserialize(reader);

                 // Write out the properties of the object.
                 Console.Write(i.qwFillTransId);
             }



        }


    }
}

This is the error I am getting at runtime:

Unhandled Exception: System.InvalidOperationException: There is an error in XML
document (2, 2). ---> System.InvalidOperationException: <TradeFills xmlns=''> wa
s not expected.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTradeF
ill.Read3_TradeFill()
   --- End of inner exception stack trace ---

Any help would be appreciated. I am new to c# and to XML and am not sure where I am going wrong.

A: 

I see at least two problems:

  • your XML document is not valid, it's missing the XML declaration:

  • Your XML apparently contains a collection of TradeFill objects, and you create the XmlSerializer for typeof(TradeFill). You need to create a serializer for typeof(TradeFill[]) (or typeof(List<TradeFill>), or some other collection type):

             XmlSerializer serializer = new XmlSerializer(typeof(TradeFill[]), new XmlRootAttribute("TradeFills"));
             // Reading the XML document requires a FileStream.
             Stream reader = new FileStream(filename, FileMode.Open);
    
    
    
         // Declare an object variable of the type to be deserialized.
         TradeFill[] tradeFills;
    
    
         // Call the Deserialize method to restore the object's state.
         tradeFills = (TradeFill[])serializer.Deserialize(reader);
    

Note the use of XmlRootAttribute to specify the root element: if unspecified, the default root element would have been "ArrayOfFillItem"

Thomas Levesque
A: 

You should deserialize based on the TradeFills type - NOT the TradeFill type. Your XML defines an instance of TradeFills after all!

So when you instantiate your deserializer, use

private void DeserializeObject(string filename)
{
    Console.WriteLine("Reading with Stream");
    // Create an instance of the XmlSerializer.
    XmlSerializer serializer = new XmlSerializer(typeof(TradeFills));
                                                        **********

TradeFills - not TradeFill !

marc_s