Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Export object collection to XML file
Message
De
20/02/2009 12:19:26
 
 
À
20/02/2009 11:47:49
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01383110
Message ID:
01383118
Vues:
35
>Using C#...I have a small app that creates objects from a class, and adds them to an object collection and it does some UI stuff along the way to display certain values from the currently selected object in the collection (Using WPF UI). So I want to add the ability to let the user save his object collection to a file, so they can load it back the next time they run the app.
>
>I'm guessing XML is an obvious way to go, but I know nothing about how to do it. So, how do you export or dump a collection of objects to an xml file, and then, perhaps more importantly, how would I then read that XML file back into my app to re-create the collection?
Hi,
Very basically:
 public static void ObjectToBinaryFile(Object o, string fileName)
        {
                 using (FileStream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    new BinaryFormatter().Serialize(fStream, o);
                }
          }

        public static object BinaryFileToObject(string filename)
        {
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                return  new BinaryFormatter().Deserialize(fs);
            }
        }
If you want XML version then use the XMLSerializer instead....
HTH,
Viv
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform