6

I were using FileStream to Serialize an Object to Xml and Save to the disk

Stream str = new FileStream(@"serializedstate.xml", FileMode.OpenOrCreate)
XmlSerializer x = new XmlSerializer(typeof(GridState));
x.Serialize(str, new GridState
        {
            GridName= txtGridName.Text,
            GridColumns = GetGridColumnStates()
        });

This works fine and Xml file is generated on disk. How do I save serialized Object as Xml into a Sql Server 2008 database's XML column using Linq to SQL? And how to deserialize the same from database?

2 Answers 2

7

To serialize into an XElement

        XmlSerializer x = new XmlSerializer(typeof(GridState));
        XDocument doc = new XDocument();

        using (XmlWriter xw = doc.CreateWriter())
        {
            x.Serialize(xw, new GridState
                {
                    GridName= txtGridName.Text,
                    GridColumns = GetGridColumnStates()
                });
            xw.Close();
        }

        XElement el = doc.Root;

To deserialize

        using (XmlReader xr = el.CreateReader())
        {
            GridState myDeserializedObject = x.Deserialize(xr) as GridState;
            xr.Close();
        }
Sign up to request clarification or add additional context in comments.

Comments

0

In sql you must have colum type XML and in linq the type of Colum must be XElement than you can manipulate throw Linq.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.