XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Loading file

Last post 10/7/2007 2:59 PM by blahbbb. 4 replies.
  • 10/7/2007 12:27 AM

    Loading file

    I succesfully saved an array of polygon class to a file.  Im having trouble reading the array from the file.

    I can get the first one but im not sure how to iterate to the next one on the file.


    I put  //******* where im having troubles.

     


     


    public bool Load(List ret)
    {

    Polygon tmp = new Polygon();
    StorageDevice device = StorageDevice.ShowStorageDeviceGuide();


    // Open a storage container
    StorageContainer container = device.OpenContainer("AISavedMaps");

    // Get the path of the save game
    string filename = Path.Combine(container.Path, "saving.xml");

    // Check to see if the save exists
    if (!File.Exists(filename))
    // Notify the user there is no save
    return false;

    ret.Clear();
    // Open the file
    FileStream stream = File.Open(filename, FileMode.OpenOrCreate,
    FileAccess.Read);
    // Read the data from the file

    // problem area,
    //****************************************************
    XmlSerializer serializer = new XmlSerializer(typeof(Polygon));
    //while(stream.Read()
    // XmlReader reader = new XmlTextReader(stream);
    while (stream.Position != stream.Length)
    {
    tmp = (Polygon)serializer.Deserialize(stream);
    ret.Add(tmp);
    }

    //****************************************************
    // Close the file
    stream.Close();



    // or something like this (also crashes)

    XmlSerializer serializer = new XmlSerializer(typeof(Polygon));

    //while(stream.Read()
    XmlReader reader = new XmlTextReader(stream);
    while (serializer.CanDeserialize(reader))
    {

    ret.Add((Polygon)serializer.Deserialize(reader));
    //serializer.d
    }
  • 10/7/2007 1:36 AM In reply to

    Re: Loading file

    This should be throwing an exception - what does it say? Also, what does the inner exception say? Finally, can you post your file contents? I suspect you're using the serializer incorrectly. The serializer probably thinks your entire file is one big polygon, when you have it setup as a list of polygons. Maybe not, but serializers usually work on entire files.

  • 10/7/2007 3:58 AM In reply to

    Re: Loading file

    It doesnt have to be serialized , xml. This is just the first sample code i found on the internet. What i want to do is save a List of polygons and be able to load them back into the List from the saved file.


    /// polygon class/////////////////////////
    public class Polygon : SaveGameStorage
    {
    public Color BoxColor;
    public Vector2 Min, Max;
    public float Height, Width;
    public bool isActive;
    public bool Moving;
    ///////////////////////////////////////////

    sorry i dont know the tags to post code
    // save and load function/////////////////
    ////////////////////////////////////////////

    public class SaveGameStorage
    {

    public void Save(List sg)
    {
    StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
    // Open a storage container
    StorageContainer container = device.OpenContainer("AISavedMaps");
    // Get the path of the save game
    string filename = Path.Combine(container.Path, "saving.xml");

    // Open the file, creating it if necessary
    FileStream stream = File.Open(filename, FileMode.OpenOrCreate);

    // Convert the object to XML data and put it in the stream
    XmlSerializer serializer = new XmlSerializer(typeof(Polygon));
    for(int i = 0; i
    if(sg[i].isActive)
    serializer.Serialize(stream, sg[i]);
    // stream.BeginWrite(
    // Close the file

    stream.Close();

    // Dispose the container, to commit changes
    container.Dispose();
    }


    public bool Load(List ret)
    {

    Polygon tmp = new Polygon();
    StorageDevice device = StorageDevice.ShowStorageDeviceGuide();


    // Open a storage container
    StorageContainer container = device.OpenContainer("AISavedMaps");

    // Get the path of the save game
    string filename = Path.Combine(container.Path, "saving.xml");

    // Check to see if the save exists
    if (!File.Exists(filename))
    // Notify the user there is no save
    return false;

    ret.Clear();
    // Open the file
    FileStream stream = File.Open(filename, FileMode.OpenOrCreate,
    FileAccess.Read);
    // Read the data from the file

    XmlSerializer serializer = new XmlSerializer(typeof(Polygon));

    //while(stream.Read()
    XmlReader reader = new XmlTextReader(stream);
    while (serializer.CanDeserialize(reader))
    {

    ret.Add((Polygon)serializer.Deserialize(reader));
    //serializer.d
    }
    // Close the file
    stream.Close();

    // Dispose the container
    container.Dispose();

    return true;
    }
  • 10/7/2007 11:42 AM In reply to

    Re: Loading file

    Answer
    Reply Quote
    The serializer converts a single object into a file or creates a single object from a file. So, you just need to serialize the List<> and deserialize the file into a List<>.

    eg.

    public static bool Save(string filename, List<Polygon> polygonList)
    {
    Stream stream = File.Create(filename);
    XmlSerializer serializer = new XmlSerializer(typeof(List<Polygon>));
    serializer.Serialize(stream, polygonList);
    stream.Close();

    return true;
    }

    public static bool Load(string filename, List<Polygon> polygonList)
    {
    Stream stream = File.OpenRead(filename);
    XmlSerializer serializer = new XmlSerializer(typeof(List<Polygon>));
    polygonList = (List<Polygon>)serializer.Deserialize(stream);
    stream.Close();

    return true;
    }

  • 10/7/2007 2:59 PM In reply to

    Re: Loading file

    thank you that worked :-)


    [source]
    // posting this for refrence
    // List == List(polygon) list class anyways

    public void Save(string name, List sg)
    {
    StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
    // Open a storage container
    StorageContainer container = device.OpenContainer("AISavedMaps");
    // Get the path of the save game
    string filename = Path.Combine(container.Path, name);

    // Open the file, creating it if necessary
    FileStream stream = File.Open(filename, FileMode.Create);

    // Convert the object to XML data and put it in the stream
    XmlSerializer serializer = new XmlSerializer(typeof(List));

    serializer.Serialize(stream, sg);

    // Close the file

    stream.Close();

    // Dispose the container, to commit changes
    container.Dispose();
    }


    public List Load(string name)
    {


    StorageDevice device = StorageDevice.ShowStorageDeviceGuide();

    List ret = new List();
    // Open a storage container
    StorageContainer container = device.OpenContainer("AISavedMaps");

    // Get the path of the save game
    string filename = Path.Combine(container.Path, name);

    // Check to see if the save exists
    if (!File.Exists(filename))
    // Notify the user there is no save
    return ret;

    ret.Clear();
    // Open the file
    FileStream stream = File.Open(filename, FileMode.OpenOrCreate,
    FileAccess.Read);
    // Read the data from the file

    XmlSerializer serializer = new XmlSerializer(typeof(List));


    ret = (List)serializer.Deserialize(stream);


    // Close the file
    stream.Close();

    // Dispose the container
    container.Dispose();

    return ret;
    }
    [/source]
Page 1 of 1 (5 items) Previous Next