Connecting Tech Pros Worldwide Forums | Help | Site Map

How to remove xmlns:xsd at root tag via XmlSerialization

=?ISO-8859-1?Q?Norbert_P=FCrringer?=
Guest
 
Posts: n/a
#1: Jun 27 '08
Hello,

I use Xml serialization to serialize an object into an xml file.

My root tag is defined as following:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d",
"2.0.50727.42")]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[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
Guest
 
Posts: n/a
#2: Jun 27 '08

re: How to remove xmlns:xsd at root tag via XmlSerialization


Norbert Pürringer wrote:
Quote:
<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.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
=?Utf-8?B?RmVyZGluYW5kIFByYW50bA==?=
Guest
 
Posts: n/a
#3: Jun 27 '08

re: How to remove xmlns:xsd at root tag via XmlSerialization


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);
}
}

Closed Thread