Groups | Blog | Home
all groups > dotnet xml > october 2003 >

dotnet xml : Removing the default root attributes when serializing


James Newton-King
10/27/2003 2:29:52 AM
In an application I'm making I've got a config object which I would like to
serialize to the XML format in a .config file which would then be loaded the
next time the application starts up using Configuration class. I've got all
the elements and attributes correctly named but the default namespaces that
get added when an object is serialized are causing the program to crash when
it loads the .config file.

These are the attributes I mean -
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

I know there are easier ways of doing this but I'd like to see if I can get
it working this way.

Thanks

James

Dino Chiesa [Microsoft]
10/28/2003 2:37:39 PM
This is a FAQ.

To remove the default namespaces, add a namespace "" pointing to "" to the
XmlSerializerNamespaces:

System.Xml.Serialization.XmlSerializer s = new
System.Xml.Serialization.XmlSerializer(typeof(MyType));
MyType x= new MyType();

System.Xml.Serialization.XmlSerializerNamespaces ns = new
System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add( "", "" );

s.Serialize(System.Console.Out, x, ns); // serialize self to
Console.Out



-Dino
--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ o n l i n e . m i c r o s o f t . c o m



[quoted text, click to view]

AddThis Social Bookmark Button