public class InventoryItem
{
public string ItemName { get; set; }
public string ItemNumber { get; set; }
public decimal Price { get; set; }
public int QtyOnHand { get; set; }
public InventoryItem()
{
}
public static void Save(List<InventoryItem> List, string FileName)
{
//create a backup
string backupName = Path.ChangeExtension(FileName, ".old");
if (File.Exists(FileName))
{
if (File.Exists(backupName))
File.Delete(backupName);
File.Move(FileName, backupName);
}
using (FileStream fs = new FileStream(FileName, FileMode.Create))
{
XmlSerializer ser = new XmlSerializer(typeof(List<InventoryItem>));
ser.Serialize(fs, List);
fs.Flush();
fs.Close();
}
}
public static List<InventoryItem> Load(string FileName)
{
if (!File.Exists(FileName))
throw new FileNotFoundException("The inventory file could not be found", FileName);
List<InventoryItem> result;
using (FileStream fs = new FileStream(FileName, FileMode.Open))
{
XmlSerializer ser = new XmlSerializer(typeof(List<InventoryItem>));
result = (List<InventoryItem>)ser.Deserialize(fs);
}
return result;
}
}
C#: Serialization/Deserialization of List<T>
Started by scottk, Jul 15 2009 03:57 PM
5 replies to this topic
#1
Posted 15 July 2009 - 03:57 PM
Here is an example of serialization and deserialization of business objects. This example writes the xml out to a file:
|
|
|
#2
Guest_Jordan_*
Posted 20 July 2009 - 04:28 AM
Guest_Jordan_*
Hey Scottk, I moved this to the code section since it isn't a tutorial. Cool bit of code, thanks for sharing! :)
Edited by Jordan, 20 July 2009 - 03:43 PM.
#3
Posted 20 July 2009 - 04:53 AM
Ah, I did miss the code snippet forum! Thanks for cleaning up behind me -- I'll make sure to get future posts in the right forum.
Thanks
Thanks
#4
Posted 20 July 2009 - 12:23 PM
What are business objects? Is that a special type of object or just a name you made up for this object?
#5
Posted 20 July 2009 - 12:24 PM
Neither, it is a concept: Business object (computer science) - Wikipedia, the free encyclopedia
[edit]Well I guess that would be considered a special type of object[/edit]
[edit]Well I guess that would be considered a special type of object[/edit]
#6
Posted 20 July 2009 - 05:50 PM


Sign In
Create Account


Back to top









