locked
Help on serializing List<T> RRS feed

  • Question

  • Dear all,

    I have a class Name Recipe which is serializable and for which I am able to serialize a single Recipe which looks like as follow :

    <?xml version="1.0" encoding="utf-8"?>
    <Recipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <ClassId>66</ClassId>
      <ClassName>Profile ext1</ClassName>
      <WhileRunning>0</WhileRunning>
      <LineId>PSUBETA</LineId>
      <Id>80</Id>
      <RecipeClassId>66</RecipeClassId>
      <HasChild>false</HasChild>
      <ChildCount>0</ChildCount>
      <Name>PA12 RILSAN MA4411</Name>
      <RecipeClassName>Profile ext1</RecipeClassName>
      <Status>1</Status>
      <IsModified>false</IsModified>
      <LastUse>2009-09-01T09:21:15.783</LastUse>
      <LastChanged>2009-09-28T10:12:59.077</LastChanged>
      <User>Sys</User>
      <Parameters>
        <RecipeParameter>
          <Name>Zone 1 temperature setp.</Name>
          <Machine>Extruder 1</Machine>
          <Information>?</Information>
          <InformationIndex>1173</InformationIndex>
          <Unit>°C</Unit>
          <MinVal>0</MinVal>
          <MaxVal>450</MaxVal>
          <Variable>ext1#TempLoop1Ns</Variable>
          <VariableId>1173</VariableId>
          <Value>69</Value>
          <PreviousValue>170</PreviousValue>
          <Type>Analog</Type>
          <RecipeId>80</RecipeId>
          <RecipeClassId>66</RecipeClassId>
          <LineId>PSUBETA</LineId>
          <GroupeName />
          <IsModified>false</IsModified>
        </RecipeParameter>
        <RecipeParameter>
          <Name>Zone 2 temperature setp.</Name>
          <Machine>Extruder 1</Machine>
          <Information>?</Information>
          <InformationIndex>1139</InformationIndex>
          <Unit>°C</Unit>
          <MinVal>0</MinVal>
          <MaxVal>450</MaxVal>
          <Variable>ext1#TempLoop2Ns</Variable>
          <VariableId>1139</VariableId>
          <Value>190</Value>
          <PreviousValue>190</PreviousValue>
          <Type>Analog</Type>
          <RecipeId>80</RecipeId>
          <RecipeClassId>66</RecipeClassId>
          <LineId>PSUBETA</LineId>
          <GroupeName />
          <IsModified>false</IsModified>
        </RecipeParameter>
        
      </Parameters>
      <ChildList />
      <IsSystem>false</IsSystem>
      <Description />
      <MemberOf>Others</MemberOf>
    </Recipe>
    Out of this what I try to serialize is a List<Recipe> object. In order to do that I have define a new class as follow :

     [Serializable()]
        [XmlRoot("Recipes")]
        public class Recipes : List<Recipe>, ICloneable
        {
    
            public Recipes() { }
    
    
            #region ICloneable Members
    
            public object Clone()
            {
                return this.MemberwiseClone();
            }
    
            #endregion
        }
    Then when seriliszing it I am doing it as follow :
     _recList.Add(new Recipe);
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer( _recList.GetType() );
                    TextWriter tw = new StreamWriter(dlg.FileName);
                    serializer.Serialize(tw, rec);
    Doing so I get the following error :
    Unable to cast object of type 'Maillefer.Nomos.Types.Recipe' to type 'System.Collections.Generic.List`1[Maillefer.Nomos.Types.Recipe]'

    What did I miss here ?

    thnaks for help
    regards
    serge

    Your experience is build from the one of others
    Monday, September 28, 2009 9:29 AM

Answers

  • It looks like you're attempting to serialize a Recipe instance instead of a Recipes instance. The final line should be serializer.Serialize(tw, _recList).
    • Marked as answer by nobugz Monday, September 28, 2009 12:41 PM
    Monday, September 28, 2009 12:24 PM

All replies

  • It looks like you're attempting to serialize a Recipe instance instead of a Recipes instance. The final line should be serializer.Serialize(tw, _recList).
    • Marked as answer by nobugz Monday, September 28, 2009 12:41 PM
    Monday, September 28, 2009 12:24 PM
  • Ok it works now but I get the output as follow :

    - <ArrayOfRecipe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <Recipe>
      <ClassId>66</ClassId> 
      <ClassName>Profile ext1</ClassName> 
      <WhileRunning>0</WhileRunning> 
      <LineId>PSUBETA</LineId> 
      <Id>72</Id> 
      <RecipeClassId>66</RecipeClassId> 
      <HasChild>false</HasChild> 
      <ChildCount>0</ChildCount> 
      <Name>[System]</Name> 
      <RecipeClassName>Profile ext1</RecipeClassName> 
      <Status>4</Status> 
      <IsModified>false</IsModified> 
      <LastUse>2009-09-21T15:04:21.917</LastUse> 
      <LastChanged>2009-09-10T10:47:52.087</LastChanged> 
      <User>Sys</User> 
    + <Parameters>
      </Parameters>
      <ChildList /> 
      <IsSystem>false</IsSystem> 
      <Description /> 
      <MemberOf>Others</MemberOf> 
      </Recipe>
      </ArrayOfRecipe>

    As you can see, my List of Recipe start with <ArrayOfRecipe>, how can I define my own element at the root instead of this.
    I send it to file as follow :

    XmlSerializer xmlSerialization = new System.Xml.Serialization.XmlSerializer(_RecListToExport.GetType());

                    TextWriter tw = new StreamWriter(outputfile);
                    xmlSerialization.Serialize(tw, _RecListToExport);

    Thnaks for help
    regards
    serge


    Your experience is build from the one of others
    Monday, September 28, 2009 1:14 PM
  • Are you certain you've applied the XmlRootAttribute attribute to your class? ArrayOfRecipe is the default type name the XmlSerializer would use for List<Recipe> if you didn't provide an alternative.
    Monday, September 28, 2009 2:59 PM