0

I have created a Class called "VNCVars.cs" and I want to be able to Save the data associated with all the Variables inside VNCVars to a XML file so I can load them back in on startup. I have another class called "SaveData.cs" which contains the code to generate the XML file.

I have code written that runs but my XML file is always empty.....can somebody point out what I have missed out??

public class VNCVars
{
    //Global Variables for VNC 1 Location
    //VNC File Location 1 Get and Set routines
    private static string strVNC1Location;
    public static string VNC1Location
    {
        get { return strVNC1Location; }
        set { strVNC1Location = value; }
    }


    //Global Variables for VNC 2 Location
    //VNC File Location 2 Get and Set routines
    private static string strVNC2Location;
    public static string VNC2Location
    {
        get { return strVNC2Location; }
        set { strVNC2Location = value; }
    }


    //Global Variables for VNC 3 Location
    //VNC File Location 3 Get and Set routines
    private static string strVNC3Location;
    public static string VNC3Location
    {
        get { return strVNC3Location; }
        set { strVNC3Location = value; }
    }
}

public class SaveXML
{

    public static void SaveData()
    {
        var SaveData = new VNCVars();

        XmlSerializer sr = new XmlSerializer(typeof(VNCVars));
        TextWriter writer = new StreamWriter(@"c:\Fanuc\SetupVars.xml");
        sr.Serialize(writer, SaveData);
        writer.Close();
    }


}

Then finally on my form I currently just have a button and on the click the following occurs...

    private void button2_Click(object sender, EventArgs e)
    {
        SaveXML.SaveData();
    }

Any help greatly appreciated....

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

2 Answers2

1

You should use instance properties instead of static properties.

You can then use the singleton pattern to keep only one instance of this class.

Johann Blais
  • 9,389
  • 6
  • 45
  • 65
  • `Singleton` in C#: https://msdn.microsoft.com/en-gb/library/ff650316.aspx. NB: Singletons are considered an anit-pattern in many scenarios, so please use caution: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons. Depending on your scenario, consider `Dependency Injection`: http://gameprogrammingpatterns.com/singleton.html – JohnLBevan Jan 08 '16 at 12:51
0

If you need to have static variables and don't want to make this class a singleton, the way around is to wrap static variables:

[XmlElement("VNC1Location")]
public string VNC1LocationLocal
{
    get
    {
        return VNC1Location;
    }
    set
    {
        VNC1Location = value;
    }
}
Sebastian Budka
  • 396
  • 1
  • 8
  • 19
  • Ok, I have added this into my VNCVars class and when I generate the XML file VNC1LocationLocal is there with the Value that I have entered into Public Static String VNCLocation1. So as well as private static string strVNC1Location; public static string VNC1Location { get { return strVNC1Location; } set { strVNC1Location = value; } } Do i need the corresponding public string VNC1LocationLocal { get { return VNC1Location; } set { VNC1Location = value; } } to make it work?? – jeuhyis Jan 08 '16 at 13:24
  • Yes, you need `VNC1LocationLocal` property (name is irrelevant) to serialize `VNC1Location` static property. I forgot that my code will generate `` element in xml. You need to add `XmlElement` attribute with desired xml name to generate proper xml. I have corrected my answer. – Sebastian Budka Jan 09 '16 at 07:26
  • Does it work for you? Or maybe you have more questions? – Sebastian Budka Jan 11 '16 at 09:13