简体   繁体   中英

How to Serialize List<CustomClass> into XML

I have serialize List<Server> servers .

Server class has this variables:

public class Server
{
    string serverName { get; set; }
    string dnsIP { get; set; }
    Game game { get; set; }

and game class:

public class Game
{
    public enum Genre { FPS, RTS, RPG, MMO, MOBA, TPS, Sandbox, Other };
    private string gameName { get; set; }
    private Genre genre { get; set; }

and when i try to serialize like this:

 private void saveServersToolStripMenuItem_Click(object sender, EventArgs e)
        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "XML files (*.xml)|*.xml";
            saveFileDialog.InitialDirectory = ".";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Stream myStream = null;
                if ((myStream = saveFileDialog.OpenFile()) != null)
                {
                    var serializer = new XmlSerializer(typeof(List<Server>));
                    serializer.Serialize(myStream, Arrays.servers);
                    myStream.Close();
                }
                saveFileDialog.Dispose();
            }

        }

i got XML file like this:

<?xml version="1.0"?>
<ArrayOfServer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Server />
  <Server />
</ArrayOfServer>

How I can get all information like serverName , dnsIP , gameName , genre.

Make your properties public. The Serializer will need to be able to read your properties otherwise it does not know how to serialize the object.

public class Server
{
    public string serverName { get; set; }
    public string dnsIP { get; set; }
    public Game game { get; set; }
}

public class Game
{
    public enum Genre { FPS, RTS, RPG, MMO, MOBA, TPS, Sandbox, Other };

    public string gameName { get; set; }        
    public Genre genre { get; set; }
}

If you have some reason for making these properties private you can also use the IXmlSerializable interface and override the GetSchema, ReadXml and WriteXml methods to get access to your private properties. However if you do that you're going to loose all the ease of use for the xml serializer.

You can read more about the IXmlSerializable interface on MSDN http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

Your Server class doesn't have public properties, it only has internal properties - XmlSerializer only works with public properties/fields.

XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport.

If you need to use fields I would recommend switching to DataContractSerializer instead, it's more flexible in this regard.

However, if that isn't the case you would just need to update the fields to have the public access modifier ie

public class Server
{
    public string serverName { get; set; }
    ...
}

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