Home
Manage Your Code
Snippet: Read back a collection of objects from a XML string (C#)
Title: Read back a collection of objects from a XML string Language: C#
Description: Reads back a collection of objects from a string that contains a collection saved as XML Views: 388
Author: Kai Bohli Date Added: 2/26/2008
Copy Code  
1        public List<Package> GetPackagesFromString(string xmlstring)
2        {
3            List<Package> packages = new List<Package>();
4            XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Package>));
5
6            using (StringReader sr = new StringReader(xmlstring))
7            {
8                packages = (List<Package>) xmlFormat.Deserialize(sr);
9                sr.Close();
10            }
11
12            return packages;
13        }
14
15
Usage
string collstring = xmlPackageCollectionTest(); // save the collection as xml first
bsPackageCollection.DataSource = GetPackagesFromString(collstring); // read back the collection as a list<> of objects