I would like to save an instance of a c#.NET class in SQL for later retrieval. I am able to use LINQ to SQL to add a record complete with all the xml that makes up the class.
Now how do I retrieve that xml and reconstruct the class object instance?
I would like to save an instance of a c#.NET class in SQL for later retrieval. I am able to use LINQ to SQL to add a record complete with all the xml that makes up the class.
Now how do I retrieve that xml and reconstruct the class object instance?
I would recommend xml serialization and deserialization.
If I have a class Person defined with:
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
saving it/loading it from XML is as simple as:
Person donald = new Person{
ID=1,
FirstName="Donald",
LastName="Duck",
DateOfBirth=new DateTime(1950,1,1)};
//create a xml serializer with the required type
XmlSerializer xs=new XmlSerializer(typeof(Person));
//open a stream to the file, and save the instance
TextWriter tw = new StreamWriter(@"C:\donald.xml");
xs.Serialize(tw, donald);
tw.Close();
//open a reader stream to the file, and just load the instance.
TextReader tr = new StreamReader(@"C:\donald.xml");
Person donald2 = (Person) xs.Deserialize(tr);
tr.Close();
Caveat: This saves only the public properties/fields of a class, as XML Elements. If you want to exercise addition control over the generated XML, take a look at the attributes in the System.Xml.Serialization namespace (XmlAttributeAttribute is my personal favorite),
Serialize your object to an XML string:
public static string ToXml<T>(T obj)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (Stream stream = new MemoryStream())
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
new XmlSerializer(obj.GetType()).Serialize(writer, obj);
writer.Flush();
stream.Flush();
stream.Position = 0;
using (TextReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
Deserialize an XML string into an object:
public static T FromXml<T>(string xml)
{
using (TextReader reader = new StringReader(xml))
{
try
{
return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
}
catch (InvalidOperationException)
{
// string passed is not XML, return default
return default(T);
}
}
}
As others mention, serialization will do the trick, but be aware of the formatters/serializer, otherwise this :
<?xml version="1.0" encoding="utf-8" ?>
will be part of your serialization. Whenever possible, use the DataContractSerializer.
I highly recommend you to see this : http://stackoverflow.com/questions/933664/net-xml-serialization-without-xml-root-node before doing anything.
HTH