简体   繁体   中英

How to Serialize Object to Xml

The class I want to store:

[Serializable]
public class Storagee
{
    int tabCount;
    List<string> tabNames;
    List<EachItemListHolder> eachItemsHolder;

    public void PreSetting(int count, List<string> strings, List<EachItemListHolder> items)
    {
        tabCount = count;
        tabNames = strings;
        eachItemsHolder = items;
    }

    public void PreSetting(int count )  //debug purpose
    {
        tabCount = count;       
    }

    public int GetTabCount() { return tabCount; }
    public List<string> GetTabNames() { return tabNames; }
    public List<EachItemListHolder> GetListEachItemListHolder() { return eachItemsHolder; }

}

Serializing class:

namespace Book
{
    class SaveAndLoad
    {
        public void SaveAll(Storagee str)
        {
            var path = @"C:\Temp\myserializationtest.xml";
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                XmlSerializer xSer = new XmlSerializer(typeof(Storagee));
                xSer.Serialize(fs, str);
            }

        }

        public Storagee LoadAll()
        {
            var path = @"C:\Temp\myserializationtest.xml";

            using (FileStream fs = new FileStream(path, FileMode.Open)) //double  
            {
                XmlSerializer _xSer = new XmlSerializer(typeof(Storagee));

                var myObject = _xSer.Deserialize(fs);

                return (Storagee)myObject;

            }
        }

    }
}

Main method (Window form):

class Book
{
    List<EachTab> eachTabs;
    Storagee storagee;
    SaveAndLoad saveAndLoad;
    eachTabs = new List<EachTab>();
    storagee = new Storagee();
    saveAndLoad = new SaveAndLoad();


    void Saving() 
    {
        int count = UserTab.TabCount;     // tab counts
        storagee.PreSetting(count);
        saveAndLoad.SaveAll(storagee); 
    }

} 

It makes xml file but doesn't save data.

I tried the serializing code in different project and it worked.

but it doesn't in this solution

since I'm kind of new to coding I don't know what the problem is

especially serializing part.

serializing codes are copied and pasted with little tweak

It makes xml file but doesn't save data.

It doesn't save any data because your class does not provide any data that it can serialize. XmlSerializer only serializes public fields and properties and the Storagee class doesn't have any.

You could, for example, change your public getter methods to public properties:

public int TabCount { get; set; }
public List<string> TabNames { get; set; }
public List<string> EachItemsHolder { get; set; }

Alternatively, if using public properties is not an option, you could also look into using custom serialization by implementing IXmlSerializable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM