Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# deserialize xml file to form List<T>

I have the following class and I am trying to serialize and deserialize from and to a XML file:

public class cUrlData
{
    public string ProgramName {get;set;}
    public string ExeName { get; set; }
    public string Category { get; set; }
    public string URL { get; set; }

    public cUrlData()
    {
    }

    public void Add(string ProgramName, string ExeName, string Category, string ProgramURL)
    {
        this.ProgramName = ProgramName;
        this.ExeName = ExeName;
        this.URL = ProgramURL;
        this.Category = Category;
    }

}

I have been using the following code to test this out:

public List<cUrlData> SoftwareData = new List<cUrlData>();
cUrlData urlData;
cXml xml;

public frmUpdater()
{
    InitializeComponent();

    xml = new cXml("data.xml", ref SoftwareData);
    xml.Load();  // NOT WORKING - SO I GENERATE MY OWN DATA BELOW

    // Set up some test data to work with
    urlData = new cUrlData();
    urlData.Add("Program1",
                            "Program1.exe",
                            "myDownloads",
                            "http://www.source.com/program1.exe");

    SoftwareData.Add(urlData);

    urlData = new cUrlData();
    urlData.Add("Program2",
                            "Program2.exe",
                            "myDownloads",
                            "http://www.source.com/program2.exe");

    SoftwareData.Add(urlData);

    urlData = new cUrlData();
    urlData.Add("Program3",
                            "Program3.exe",
                            "myDownloads",
                            "http://www.source.com/program3.exe");
    SoftwareData.Add(urlData);

}

The problem I am having is to find a reliable way to convert the List to and from a XML file. I currently looping through the list of classes and manually creating the xml file node by node and doing the same when reading it from the xml file to the classes but this is prone to errors. I have attempted to get the following code to read the file but to no avail and would be grateful for some advice as I am sure it is a coding problem!

public void Load() {
    XmlSerializer serializer = new XmlSerializer(typeof(List<cUrlData>));

    using (XmlReader reader = XmlReader.Create("data.xml"))
    {
        cUrlData myXmlClass = (cUrlData)serializer.Deserialize(reader);
    }
}

Once loaded, I want to try and get it to write back to an xml file. Again, in a similar way to the code above.

Thank you

like image 496
Belliez Avatar asked Jan 21 '23 00:01

Belliez


1 Answers

Here should be a general solution to get you started on saving and loading

private void SaveData(List<cUrlData> SoftwareData)
{
    try
    {
        using (TextWriter reader = new StreamWriter("data.xml"))
        {
            (new XmlSerializer(typeof(List<cUrlData>))).Serialize(reader, SoftwareData);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}
private List<cUrlData> LoadData()
{
    List<cUrlData> mysXmlClass = null;

    try
    {
        using (TextReader reader = new StreamReader("data.xml"))
        {
            object myXmlClass = (object)(new XmlSerializer(typeof(List<cUrlData>))).Deserialize(reader);
            mysXmlClass = (List<cUrlData>)myXmlClass;
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

    return mysXmlClass;
}

It does need tided up some. I used TextReaders and StreamReaders as I am familiar with those.

like image 165
Gauthier Avatar answered Jan 29 '23 11:01

Gauthier