Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 namespaces 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

like image 462
BrianK Avatar asked Apr 07 '10 02:04

BrianK


1 Answers

No, these namespaces will not slow down the deserialisation. Those URIs are not Web endpoints that the serialiser visits: they are just identifiers -- labels which happen to use the Web URI scheme in order to guarantee uniqueness. You can safely leave them in.

like image 127
itowlson Avatar answered Oct 12 '22 00:10

itowlson