XML Serialization - Remove XML-instance namespace?

K

kimiraikkonen

Hello Everyone,

When I'm serializing my objects to XML, .NET seems to put the following
namespace into certain elements:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

Does anyone know what its for? Is there a way to prevent .NET from adding
this information?

Thanks!

Withoug being sure, my guess is that the XML will take some standard
references from W3 just like being in HTML in that code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Just an idea.
 
M

Martin Honnen

Spam said:
When I'm serializing my objects to XML, .NET seems to put the following
namespace into certain elements:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

Does anyone know what its for?

XML serialization is based on W3C XSD schemas. These namespaces are the
instance namespace and the schema namespace, depending on the
serialization attributes there might be attributes like xsi:type in the
serialized markup for which the namespace declarations are necessary.
Is there a way to prevent .NET from adding
this information?

Why do you want top prevent them? Namespace declarations do no harm
usually. I think the only way to prevent them is to set up an XmlWriter
that suppresses these attributes and pass that writer to the Serialize
method.
 
J

Jarlaxle

you can prevent it by doing the following...

XmlTextWriter formatter = new XmlTextWriter(stringWriter);
XmlSerializerNamespaces namespaceSerializer= null;

if (!bNamespaces)
{
namespaceSerializer = new XmlSerializerNamespaces();
namespaceSerializer.Add("", "");
}
XmlSerializer xmlSerializer= new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(formatter, obj, namespaceSerializer);
formatter.Close();

you can also get rid of the version declaration. to do this you need to
derive a class from XmlTextWriter and override the WriteStartDocument method
to either call the base.WriteStartDocument or not. Then use that instead of
the XmltextWriter instance above.
 
G

Guest

XmlTextWriter formatter = new XmlTextWriter(stringWriter);
XmlSerializerNamespaces namespaceSerializer= null;

if (!bNamespaces)
{
namespaceSerializer = new XmlSerializerNamespaces();
namespaceSerializer.Add("", "");
}
XmlSerializer xmlSerializer= new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(formatter, obj, namespaceSerializer);
formatter.Close();

I tried inheriting XMLSerializer and overriding the "Serialize"
procedure... but it doesn't seem to execute. Do you if there is anything
special to do when overriding XMLserializer?

Thanks!
 
F

Ferdinand Prantl

If you want to get rid of both - XML declaration and those namespace
attributes you can create your XmlWriter with explicit settings (the
namespace attributes were already discussed above):

// source: object instance to serialize
// file: target file name to write the XML to
void Serialize(object source, string file)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

using (XmlWriter writer = XmlWriter.Create(file, settings))
{
XmlSerializer serializer = new XmlSerializer(source.GetType());
serializer.Serialize(writer, source, namespaces);
}
}
 
F

Ferdinand Prantl

Use the following code to get rid of the XML declaration and the
namespace declaration attributes (the latter has been suggested above
already) - explicit settings for the writer do the trick:

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

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