Go to home page mail me! RSS Feed
FoxMetrics Web Analytics

Removing declaration and namespaces from XML (serialization)

Tuesday, January 29, 2008 10:27 AM

Sometimes working with pure XML is our only option and we may just want it as clean as it can be. I usually use the XmlDocument object to load and/or create the XML and manipulate it and then at the end of the day, I'll use the InnerXml property to get pure XML, which I usually save to a file or do something with or I may just be serializing a Serializable object to XML.

The XmlDocument object by default assigns a namespace to the XML string and also includes the declaration as the first line of the XML document. I definitely do not need or use those, therefore, I need to remove them. Here is how you go about doing just that.

 

Removing the XML declaration

 

XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
{
    serializer.Serialize(xmlWriter, request);
}
string xmlText = stringWriter.ToString();

 

Removing/Setting the namespace

 

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

XmlSerializer serializer = new XmlSerializer(typeof(object));
StringWriter stringWriter = new StringWriter();
using(XmlWriter writer = new XmlTextWriterFormattedNoDeclaration(stringWriter ))
{
    serializer.Serialize(writer, this, ns);
}
string xmlText = stringWriter.ToString();

Your Comments.

  • # re: Removing declaration and namespaces from XML (serialization)

    GravatarGreat tip! THANKS!

    Left by K Voin at 3/6/2008 4:02 PM
  • # re: Removing declaration and namespaces from XML (serialization)

    GravatarReally useful!
    Thanks

    Left by DM at 6/14/2008 4:35 AM
  • # re: Removing declaration and namespaces from XML (serialization)

    Gravatar76

    Left by t at 6/18/2008 2:45 AM
  • # re: Removing declaration and namespaces from XML (serialization)

    GravatarHoly cow, thanks!

    Left by Nikki at 1/31/2009 10:15 PM
  • # re: Removing declaration and namespaces from XML (serialization)

    GravatarJust Great! thank you so much for your post

    Left by ruben ruvalcaba at 2/7/2009 4:10 PM
  • # re: Removing declaration and namespaces from XML (serialization)

    GravatarIt is very helpful....

    Left by .Net Developer at 5/27/2009 9:48 AM
  • # re: Removing declaration and namespaces from XML (serialization)

    GravatarXmlSerializerNamespaces emptyNameSpace = new XmlSerializerNamespaces();
    emptyNameSpace.Add("", "");

    serializer = new XmlSerializer(typeof(object));
    stringWriter = new StringWriter(); serializer.Serialize(stringWriter, contactDataArray, emptyNameSpace);

    Left by Harshad_India at 6/23/2009 9:03 AM
  • # re: Removing namespaces from XML (serialization)

    GravatarCorrection on the above code submitted. Another way to get rid of namespaces from xml:

    XmlSerializerNamespaces emptyNameSpace = new XmlSerializerNamespaces();
    emptyNameSpace.Add("", "");

    serializer = new XmlSerializer(typeof(object));
    stringWriter = new StringWriter(); serializer.Serialize(stringWriter, object, emptyNameSpace);

    Left by Harshad_India at 6/24/2009 1:07 AM
  • # re: Removing declaration and namespaces from XML (serialization)

    Gravatarjust what i needed, thanks dude

    Left by rob at 7/27/2009 4:30 AM

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 8 and 3 and type the answer here:

Preview Your Comment.

 
Next entries »