0

So what I want to do is pass in an object list, but I have many different ones. (Swords, Chestplates etc). How can I pass in multiple objects to the parameter?

ListToSave = testObject.ChestPlateList; that part below is the part I want to pass the object list into.

public void DeserializeFromXML(List<ChestPlate> ListToSave, string filepath)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(GameObjectData));
            FileStream fs = new FileStream(filepath, FileMode.Open);
            XmlReader reader = new XmlTextReader(fs);

            if (!reader.EOF)
            {
                GameObjectData testObject = (GameObjectData)deserializer.Deserialize(reader);
                ListToSave = testObject.ChestPlateList;
                Console.WriteLine("{0}", testObject.ChestPlateList);             
            }        
        }
4

1 回答 1

0

试试这个(未经测试)

public void SerializeToXML(GameObjectData theData, string filepath)
{
    XmlSerializer serializer = new XmlSerializer(typeof(theData));
    TextWriter writer = new StreamWriter(filepath);
    serializer.Serialize(writer, ListToSave);
}

public GameObjectData DeserializeFromXML(string filepath)
{
    GameObjectData result = null;
    XmlSerializer deserializer = new XmlSerializer(typeof(GameObjectData));
    FileStream fs = new FileStream(filepath, FileMode.Open);
    XmlReader reader = new XmlTextReader(fs);

    if (!reader.EOF)
    {
        result = (List<GameObject>)deserializer.Deserialize(reader);

        Console.WriteLine("{0}", result.ChestPlateList);              
    }   
    return result;     
}
于 2013-04-18T18:04:43.547 回答