Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

XML Serialization revisited

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Hi snaphook,

    for simplicity you have 2 function:

    Serialize(int T1target){}

    and int T1Target=Deserialize(){}

    Serialize writes the parameter to some file and Deserialize() reads it back in. For simplicity here the variable is a single int. In reality you would serialize your whole strategy class MyStrategy : Strategy
    By setting the [XmlIgnore] attribute you define which parameters to save.

    You should find examples on XmlSerialize functions either in the forum or on MSDN site or some other C# site.


    Ok.

    Once you have the Serialize and Deserialize which dipend on your parameters


    then put

    Dispose(){
    Serialize()
    ... other stuff
    base.Dispose();
    }

    in your class to automatically save the parameters

    you do then:

    [Description("Target 1in ticks")]
    [Gui.Design.DisplayName("\tT1 Target")]
    [Category("Parameters")]
    public int T1_Target
    {
    get { return t1; }
    set {
    value=Deserialize(); // instead from GUI you read in value from your file
    // this should work as the get function is called later from the GUI
    //(to show in the properties box)
    t1 = Math.Max(1, value);
    }
    }


    be careful with Deserialize,as it might be called when Initialize() is not yet called, so you are restricted concerning access to class variables. check the log etc

    regards
    andreas



    Last edited by zweistein; 04-28-2009, 06:19 AM.

    Comment


      #17
      I am just so thick and I apologize for that. Do you mind holding my hand just a bit longer?

      My Serialize function:
      protected void SerializeSettings()
      {
      Print("Saving Current Settings");//Note to output window
      DWM_AutoTrader dwmat = new DWM_AutoTrader();
      dwmat.TypeEntry = typeEntry;
      dwmat.T1_Target = t1;
      dwmat.T2_Target = t2;
      dwmat.T3_Target = t3;
      .
      .
      .
      }

      My Deserialize function:
      protected void DeserializeSettings()
      {
      Print("Retrieving Settings");//Note to output window
      DWM_AutoTrader dwmat = new DWM_AutoTrader();
      XmlSerializer mySerializer = new xmlSerializer(typeof(DWM_AutoTrader));
      using (FileStream fs = File.OpenRead(@filePath))
      {
      dwmat = (DWM_AutoTrader)mySerializer.Deserialize(fs);

      typeEntry = dwmat.TypeEntry;
      t1 = dwmat.T1_Target;
      t2 = dwmat.T2_Target;
      t3 = dwmat.T3_Target;
      .
      .
      .
      }

      This works great but you must attach the strategy and click OK for DeserializeSettings() to retrieve the saved settings. If I set the t1 properties to that below, I get an error because the DeserializeSettings() function returns void.

      Properties for t1:
      [Description("Target 1. Set to 0 for no target.")]
      [Gui.Design.DisplayName("\tT1 Target")]
      [Category("Parameters")]
      public int T1_Target
      {
      get { return t1; }
      set { value=DeserializeSettings();
      t1 = Math.Max(0, value); }
      }

      Thanks,
      Snap

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X