2010-09-22 6 views
0

Ceci est un exemple du fichier XML que je tente de désérialiserla difficulté désérialisation fichier XML en objet de classe

<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> 

Ce code J'utilise:

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); 
      } 



     } 


    } 
} 

Ceci est l'erreur Je reçois à l'exécution:

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 --- 

Toute aide serait appréciée. Je suis nouveau à C# et à XML et je ne suis pas sûr où je vais mal.

+0

Le problème est-il dans mon code ou est-ce le fichier XML? Aussi, est-ce la balise qui gâche tout? Je ne suis pas sûr de savoir pourquoi cette étiquette est là. – xbonez

Répondre

1

Vous devez deserialize en fonction du type de TradeFills - PAS type TradeFill. Votre XML définit une instance de TradeFills après tout!

Ainsi, lorsque vous instanciez votre désérialiseur, utilisez

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

TradeFills - pas TradeFill!

0

Je vois au moins deux problèmes:

  • votre document XML n'est pas valide, il manque la déclaration XML:

  • Votre XML contient apparemment une collection de TradeFill objets, et vous créez le XmlSerializer pour typeof(TradeFill). Vous devez créer un sérialiseur pour typeof(TradeFill[]) (ou typeof(List<TradeFill>), ou un autre type de collection):

      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); 
    

Notez l'utilisation de XmlRootAttribute pour spécifier l'élément racine: si non spécifié, l'élément racine par défaut aurait été "ArrayOfFillItem"

Questions connexes