Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop DataContractSerializer putting in namespace? [duplicate]

The code looks like this:

StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (XmlWriter xmlWriter = XmlWriter.Create(builder, settings))
{
    XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
    s.Serialize(xmlWriter, objectToSerialize);
}

The resulting serialized document includes namespaces, like so:

<message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" 
    xmlns="urn:something">
 ...
</message>

To remove the xsi and xsd namespaces, I can follow the answer from How to serialize an object to XML without getting xmlns=”…”?.

I want my message tag as <message> (without any namespace attributes). How can I do this?

like image 201
NetSide Avatar asked Nov 26 '22 18:11

NetSide


1 Answers

...
XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
s.Serialize(xmlWriter, objectToSerialize, ns);
like image 132
Thomas Levesque Avatar answered Dec 05 '22 00:12

Thomas Levesque