Concise XML Serialization

G

Guest

I am serializing a class to XML using the code below, but the XmlSerializer
opens the object's tag with some very verbose attributes: "<TestClass
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml
ns:xsd="http://www.w3.org/2001/XMLSchema">"

I am serializing thousands of these objects, so for performance I would like
to exclude the "xmlns:" attributes. Is there an easy way to do that with the
standard XmlSerializer, or any other .NET serializers?

XmlTextWriter writer = new XmlTextWriter(Console.Out);
XmlSerializer serializer = new XmlSerializer(typeof(TestClass));
TestClass test = new TestClass();
serializer.Serialize(writer, test);
writer.Close();
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

If you are serializing these objects to the same stream, then I would
recommend having a container object which holds all the references, and then
serializing that. It ^should^ prevent the XSI and XSD namespaces from being
replicated.

However, if you are serializing these to different streams, then there
is nothing you can do, since it needs those namespaces for the document to
be valid (and for the serializer to interpret some values as well).

Hope this helps.
 
S

sloan

I have several "collection" serialization examples at my blog:

9/21/2005 entry

(you may have to click "View More Entries" near the bottom)
 
W

Walter Wang [MSFT]

Hi Dave,

Thank you for your post.

I agree with Nicholas on this, you cannot remove those namespaces since
they're required for the serializer to work correctly.

You may found following MSDN documentation useful:

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconControllingSerializationByXmlSerializerWithAttributes.asp



Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top