Hello,
I use Xml serialization to serialize an object into an xml file.
My root tag is defined as following:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd",
"2.0.50727.42")]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[SerializableAttribute()]
[XmlType("Style")]
public class RPStyle
{
//...
}
That will produce following xml:
<?xml version="1.0" encoding="utf-16"?>
<Style xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"/>
You see, that there is an attribute xmlns:xsd. How can I get rid of
that xmlns attribute?
Thank you,
Norbert
Martin Honnen - 24 Apr 2008 16:17 GMT
> <Style xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"/>
>
> You see, that there is an attribute xmlns:xsd. How can I get rid of
> that xmlns attribute?
Are you sure it is xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
and not xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
That is the namespace declaration of the XMLSchema-instance namespace
and might be necessary to properly qualify xsi:type or xsi:nil
attributes during serialization.

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Ferdinand Prantl - 23 May 2008 11:35 GMT
Use the following code to get rid of the XML declaration and the namespace
declaration attributes:
// source: source object instance to serialize
// target: target file name to write the XML to
void Serialize(object source, string file)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(file, settings))
{
XmlSerializer serializer = new XmlSerializer(source.GetType());
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(writer, source, namespaces);
}
}