Serialize C# class directly to SQL server? - c#

can anyone suggest the best way to serialize data (a class actually) to a DB?
I am using SQL server 2008 but i presume i need to serialize the class to a string / or other data type before storing in the database?
I presume that this field needs to be text or binary??
Does SQL server 2008 (or .net 3.5) support serializing directly tothe database ??
Any help really appreciated

You can xml serialize the class into an xml field. We use this all the time for exception logging in an ETL.
Using the XmlSerializer you may want a helper method somewhere which serializes the class to a string...
public static string SerializeToXml<T>(T value)
{
StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(writer, value);
return writer.ToString();
}
Then just put the string into the db like any other.

The best way to store data in a database is in columns (per property), so that it is queryable and indexable. ORM tools will help with this.
However, it is also possible to serialize a class as a CLOB/BLOB (varchar(max)/varbinary(max) etc).
It this is what you want, avoid anything implementation-specific or version-intolerant; so in particular, don't use BinaryFormatter. Anything contract-based should work; XmlSerializer, DataContractSerializer, etc. Or for fast binary, protobuf-net might be worth a look.
But I stress; columns would be better.

Without generics (better sollution)
public static string SerializeToXml(object value)
{
StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
XmlSerializer serializer = new XmlSerializer(value.GetType());
serializer.Serialize(writer, value);
return writer.ToString();
}

I've serialized objects as XML and thrown those into the database just fine. Since we knew the max amount of text we used the varchar(max) datatype instead of getting into TEXT or Binary formats.
This was a OLTP web application and one thing we found was that using a column with an xml datatype invoked some significant cpu usage as the xml was validated on every insert. In our case the xml was never queried for anything so not having the xml query capabilities worked out ok for us.

There are couple of options:
Runtime serialization, serializable objects are marked with the
Serializable attribute, in which case the IFormatter class does all the work of
serialization. A serializable object can ISerializable, but then you will
need to implement the GetObjectData( ) method. The problem with runtime serialization is that program reading the xml data needs to have
the knowledge of the CLR types.
Xml serialization: Unline runtime serialization, you will get good interoperability in this case.
The XmlSerializer type contains the methods Serialize( ) and Deserialize( ), thus
any object can be serialized to XML and saved into the database and when you retreive it back, you
can deserialize it easily.
To read data from the database, you can use the SqlCommand class
method that executes SQL queries, namely ExecuteXmlReader( ).
ExecuteXmlReader( ) returns an instance of XmlReader and that will read your xml data.

Check out Linq-to-SQL (questions on SO, resource on MSDN) or other O-R Mapping options.

Related

When do we use XMLSerialization?

I was searching on examples as how to store data in isolated storage. In an example XmlSerializer was used. I have given the code below. Please explain the function and need for Xmlserializer.
XmlSerializer ser = new XmlSerializer(typeof(TravelReportInfo));
ser.Serialize(fs, travelReportInfo);
XmlSerialization is the process of taking an object and representing it as xml. Xml is one of the ideal formats for this since it can describe arbitrary object hierarchies.
One common use case is transfering objects over the web. If you serialize your object to xml you can include it in http requests and responses and deserialize it back to its original object on the other side. All information to reconstruct the object is found in the xml
Bellow you can see an example of a class serialized to xml
public class OrderForm
{
public DateTime OrderDate;
}
<OrderForm>
<OrderDate>12/12/01</OrderDate>
</OrderForm>

C# Object Serialization Changes

I have the following issue:
My object is being serialized as follows:
MemoryStream memorystream = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(memorystream, _policy);
CaptureData = Convert.ToBase64String(memorystream.ToArray());
And then saved to my database and de-serialized as required from the data saved in the database.
My problem now is that I can't make changes to this object, add properties etc as this breaks the deserialization.
Any suggestions on how I can add properties without breaking serialization of previous objects?
I don't think it's possible using this serializer.
If possible, use another serializer that deals with backward compatibility like protocol buffers
You can use [OptionalField] and similar to take advantage of the version tolerant serialisation built into .Net.
You need to shift from the built-in serialization , as this is, as you saw, is rigid and not scallable. Pick any other serializaiton technique that let's you maintan scallability of your types and flexibility of your system.
custom binary serialization
xml serialization
json serialization
binary serialization with protobuf (just example...)
All this, if you can do that. If not, for some architectural (historical) restrictions, just can do something like suggested in Hamlet's comment. But it sounds like a rough solution to me.

EntityObject to XML and back to Object? (via DataTable if capable)

EDIT 2
I am updating the question again to more simply explain what I need.
I need to take any EntityObject in my application, convert it to XML, which can be reconverted back to AN object later that can be queried or used in a DataSource. I cannot reconvert to the original EntityObject because the structure of that object may have changed. Is this doable?
Original Question below
Let's assume for a second that I am required by someone higher up than me (because I am) to take an EntityObject and convert it to a DataTable. The example I was sent on how to do it is from 2006 and uses a custom class (which my boss thought was built into .Net) and it doesn't work anyway.
Is there a way to do this without writing my own custom class? Something as simple as "new DataTable(myEntityObject)" would be amazing. I'm guessing no though.
EDIT
I have changed the question because I was given the criteria wrong. Mainly we want our EntityObject to be XML so it can be stored in a database and reconstructed later. Can that be done via Serialization?
To answer your updated question:
Use the XmlSerializer
public XElement XmlSerialize(object o)
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, o);
return XElement.Parse(sw.ToString());
}
}
No there is no such built-in method / class. You must write your own conversion.
You need to write a converter class for that, something like this:
public DataRow ToDataRow<T>(T entity)
public DataTable ToDataTable<T>(IEnumerable<T> entities)

Write CLR objects to XML with LINQ

I have an ObservableCollection items. I want to convert these to XML format so that I can store them for later use. I'm a little new to LINQ and I'm not sure how to do this. I know that I need to rely on the XDocument. But I'm not sure how to execute the query and get the results into a string format.
Can somebody please provide some pointers for me? It just seems like such an easy task, I'd be surprised if it couldn't be done.
Thank you,
You need Linq to XML. I can't post a real code here since I don't know the structure of your data, but here's a dummy example:
List<Person> people = ...
var doc = new XDocument(
new XElement("People",
from p in people
select new XElement("Person",
new XAttribute("Id", p.Id),
new XElement("LastName", p.LastName),
new XElement("FistName", p.FirstName))));
doc.Save("people.xml");
Note that Linq is not the only option here. Another very good approach is XML serialization.
Using the DataContractSerializer is your best bet. It's designed to do exactly what you are asking for.
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer(v=VS.95).aspx
Advantages over XMLSerializer:
Opt-in rather than opt-out properties to serialize. This mean you specify what you want serialize
Because it is opt in you can serialize not only properties, but also fields. You can even serialize non-public members such as private or protected members. And you dont need a set on a property either (however without a setter you can serialize, but not deserialize)
Is about 10% faster than XmlSerializer to serialize the data because since you don’t have full control over how it is serialize, there is a lot that can be done to optimize the serialization/deserialization process.
Can understand the SerializableAttribute and know that it needs to be serialized
More options and control over KnownTypes
Disadvantages:
No control over how the object is serialized outside of setting the name and the order
I'm not sure what you want.
You can create a XElement and convert it to a string with the ToString method.
You can do the same with XDocument.
I'm not at all familiar with Silverlight, but it sounds like you are going to need to use XML serialization. XML serialization allows you to serialize an object into an XML representation, which you can then later deserialize from XML back into an object.
Here are some tutorials and information on XML serialization in .NET:
XML Serialization
XML Serialization Using C#
XmlSerializer class

What is the fastest way to convert a class to XML

I would like to know what is the fastest and most lightweight technique to convert a fairly large class to XML. The class will have lists and arrays in it. I need to convert all this data to XML
Here is what my application does:
it will get all the information from the database using linq to enties. Then store the data in a class. Then I want to convert this class to XML. When the data is in XML I will send the XML to the browser along with the xsl stylesheet to be displayed to the user. What is the fastest way to do this.
The XmlSerializer actually creates an assembly (with an XmlSerializationWriter) that is custom made to serialize your class. You can look at the generated code by following these steps.
You only pay the price the first time it encounters a new type.
So I think that you should really go with the XmlSerializer, not only for performance, but for maintainability.
You can use a mixin-like serializer class:
public interface MXmlSerializable { }
public static class XmlSerializable {
public static string ToXml(this MXmlSerializable self) {
if (self == null) throw new ArgumentNullException();
var serializer = new XmlSerializer(self.GetType());
using (var writer = new StringWriter()) {
serializer.Serialize(writer, self);
return writer.GetStringBuilder().ToString();
}
}
}
public class Customer : MXmlSerializable {
public string Name { get; set; }
public bool Preferred { get; set; }
}
// ....
var customer = new Customer {
Name = "Guybrush Threepwood",
Preferred = true };
var xml = customer.ToXml();
The fastest way is to write the code for it yourself. That will remove any overhead, like the need to use reflection to read the properties of the object, as you can access the properties directly.
Add a method to the class that returns it's data as XML, either by returning an XDocument, the XML already formatted as a string, or you can pass an XmlWriter to the method.
By "fastest" do you mean you want the approach which will be fastest to develop? Or do you want the approach which will have the fastest execution speed?
If it's the former, I recommend just using .NET's XmlSerializer class: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Serializing a class to XML output is as simple as:
XmlSerializer serializer = new XmlSerializer(myObject.GetType());
serializer.Serialize(Response.OutputStream, myObject);
And there are various attributes you can decorate your class with to control things like whether individual properties are serialized as elements or attributes, etc.
There's a good FAQ at http://www.devolutions.net/articles/serialization.aspx also
You could use XML serialization, for example:
Foo foo = new Foo();
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
TextWriter writer = new StringWriter();
serializer.Serialize(writer, foo);
string xml = writer.ToString();
The fastest method would depend on the class, because it would be hand-written to take advantage of knowledge of the specifics of that class in ways a more general approach couldn't do.
I'd probably use XmlTextWriter rather than straight to TextWriter though. While the latter would allow for some further savings, these would be minimal compared to the better structure of XmlTextWriter, and you've already sacrificed a good bit in terms of structure and ease of maintenance as it is.
You can always slot in your super-optimised implementation of XmlWriter afterwards ;)
It sounds like a rather convoluted set-up, when you could just display the class's information on a webpage using ASP.NET MVC. Why take the extra two steps of converting it to XML, send it to the browser, and use an XSL stylesheet to display it to the user? It doesn't make sense.
I wrote a program that serialized one simple object graph to XML in different ways:
1. Using XmlSerializer
2. Using hardcoded xml serializer
30,000 documents:
XmlSerializer took : 0.9 sec
Hardcoded serializer took: 0.45 sec
I relied on XmlWriter in both cases and that adds some overhead.
Note that you can instruct Visual Studio to generate the XmlSerializer assembly during compile time in order to reduce the serialization for that first instance (otherwise an assembly is generated in runtime).

Categories