Groups | Blog | Home
all groups > dotnet xml > july 2007 >

dotnet xml : Serializer should not include namespace informations


Martin Honnen
7/13/2007 12:00:00 AM
[quoted text, click to view]

Why are those namespace declarations a problem? They might even be
necessary if descendant elements use the declared namespaces to define
types of elements (e.g. <element xsi:type="xsd:string"/>).

If you really want to get rid of them then like this:

XmlSerializerNamespaces namespaces = new
XmlSerializerNamespaces();
namespaces.Add("", "http://www.w3.org/2001/XMLSchema");
namespaces.Add("",
"http://www.w3.org/2001/XMLSchema-instance");
serializer.Serialize(writer, yourObject, namespaces);

so you create an instance of XmlSerializerNamespaces, call the Add
method with the empty string as the first argument and the namespace URL
as the second argument which you do not want to serialize, then you pass
that XmlSerializerNamespaces object as the third argument to the
Serialize method.


--

Martin Honnen --- MVP XML
thalion77 NO[at]SPAM graffiti.net
7/13/2007 3:12:43 AM
Hello there,

I have written a function serializing a certain object:

public static XmlDocument Serialize(ServiceResponse response)
{
XmlSerializer responseSerializer = new
XmlSerializer(response.GetType());

StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
try
{
responseSerializer.Serialize(writer, response, null);
}
catch (Exception ex)
{
if (ex is InvalidOperationException)
throw ex.InnerException;
else
throw ex;
}
XmlDocument xmlResponse = new XmlDocument();
xmlResponse.LoadXml(sb.ToString());
return xmlResponse;
}

What must be done, that the serializer does not include namespace
informations like in the following example:

<?xml version="1.0" encoding="utf-16"?>
<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" request="ServiceRequest">

I want instead following response:

<?xml version="1.0" encoding="utf-16"?>
<ServiceResponse request="ServiceRequest">

Thanks in advance,
Norbert
AddThis Social Bookmark Button