none
Serialization and de-serialization of C# objects using linq (to xml) to SQL datbase. RRS feed

  • Question

  • New to linq and sql

    I am looking for complete tutorial sample app that does any or all of the following options:

    Example:

    Class Parent_A{ … }

    Class Parent_B{ ..}

    Class Parent_C{ ..}

    …. and so on...

    Derived classes: of Parent_A

    child_A1:Parent_A { … }

    child_A2:Parent_A { … }

    …... to n-th child class like so: child_An:Parent_A { … }

    n = 1, 2, …

    Derived classes: of Parent_B

    child_B1:Parent_B { … }

    child_B2:Parent_B { … }

    …... to m-th child class like so: child_Bm:Parent_B { … }

    m = 1, 2....

    Option#1:

    I like to dynamically create with C# and using linq, a database that can store all C# objects ( .NET ) as XML into ONLY ONE column of one table in database. Then de-serialize back to original stored object instance type via linq. All this time, letting linq automatically create XML translation of public properties before sending it to database.

    I think it is not possible to use a single column for serializing multiple (not similar) class types because linq is strongly typed. So each column must be assigned to a given class type for serialization and

    deserialization.

    But if you have a solution, please advice.

    And if possible, provide links of where to look.

    Thank you.

    Option#2:

    Want to save C# objects to dynamically created database using linq.

    Let linq serialize objects as XML automatically.

    Then also let linq de-serialize from database and create same object instance like so:

    a) For each child object, e.g. child_A1... child_An to child_B1 to child_Bm... and so on, I can create a table, and store all same object ( note: child_A1 class type is NOT same as child_A2 class type despite same inheritance from Parent_A ).

    In this case, I can store like so:

    Table name: Child_A1_Table

    Columns and sample contents:

    Number Object Instance

    1 child_A1_object_Number_1 ( in XML )

    2 child_A1_object_Number_2 ( in XML )

    3 child_A1_object_Number_3 ( in XML )

    ….

    i child_A1_object_Number_i ( in XML )

    Table name: Child_B4_Table

    Columns and sample contents:

    Number Object Instance

    1 child_B4_object_Number_1 ( in XML )

    2 child_B4_object_Number_2 ( in XML )

    3 child_B4_object_Number_3 ( in XML )

    ….

    j child_B4_object_Number_j ( in XML )

    Obviously, I can have one large table and multiple columns where each column is assigned to a given class type.

    If there are sample tutorial codes out there, please provide links.

    I have been looking, but most searches results are not so useful so far.

    Thank you for your time.

    Friday, November 2, 2012 8:10 PM

Answers

  • I don't think that you can find exact tutorial for your task, but I can help you with serialization methods:

    public static XElement ToXElement<T>(this object obj)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (TextWriter streamWriter = new StreamWriter(memoryStream))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(streamWriter, obj);
                return XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray()));
            }
        }
    }
    
    public static T FromXElement<T>(this XElement xElement)
    {
        using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(xElement.ToString())))
        {
            var xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(memoryStream);
        }
    }
    
    
    
    XElement element = myClass.ToXElement<MyClass>();
    var newMyClass = element.FromXElement<MyClass>();

    This extension methods can help you serialize and deserialize objects using XElement


    Please mark as reply if helped.
    Also visit my blog http://msguy.net/

    • Marked as answer by Alexander Sun Thursday, November 22, 2012 6:36 AM
    Monday, November 5, 2012 8:34 AM