views:

243

answers:

1

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"&gt;

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"&gt;
  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

+7  A: 

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.

itowlson
How embarrassing is it to admit that I just learned something new from your answer? I always wondered about those namespaces.
MusiGenesis
@MusiGenesis, regarding your query, it is not embarrassing at all.
harpo
Thanks, I wondered why so many are going to so much effort to take them out.
BrianK
I encountered a time where I had to remove namespaces. For some reason they were causing a 3rd party library I was using to crash. I never got to the bottom of why, as a future release solved that issue.
Matt Ellen