Serialize a C# class to xml. Store the XML as a string in SQL Server and then restore the class late

Posted by BrianK on Stack Overflow See other posts from Stack Overflow or by BrianK
Published on 2010-04-07T02:05:10Z Indexed on 2010/04/07 2:13 UTC
Read the original article Hit count: 353

Filed under:
|
|

I want to serialize a class to xml and store that in a field in a database. I can serialize with this:

            StringWriter sw = new StringWriter();
            XmlSerializer xmlser = new XmlSerializer(typeof(MyClass));

            xmlser.Serialize(sw, myClassVariable);
            string s = sw.ToString();
            sw.Close();

Thats works, but it has the namesapces in it.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Will these slow down the deserialization because it will go out to those and verify the XML? I got rid of the namespaces by creating a blank XmlSerializerNamespaces and using that to serialize, but then the xml still had namespaces around integer variables:

<anyType xmlns:q1="http://www.w3.org/2001/XMLSchema"
         d3p1:type="q1:int"
         xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
  3
</anyType> 

My question is: Is it necessary to have the namesapces for deserialization and if not, how to get rid of them? How do I tell it fields are ints so it doesnt put in "anytype"

Thanks, Brian

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET