Get rid of XML Namespaces and Headers
by Hans on 2012/05/17 07:20
in Categories: C#, Xml

There are times when I have had to pass xml objects to and from remote systems that simply do not like headers and namespaces. Include the xml header and/or namespaces and the request simply fails. Here is an example of a typical xml object showing the xml header and namespaces.

<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Joe</FirstName>
  <LastName>Soap</LastName>
  <Age>20</Age>
</Person>

The C# code to produce the above xml looks something like this.

We have a Person object:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestProject
{
    [System.Serializable]
    public class Person
    {
        public Person()
        {
        }

        public Person( string firstname, string lastname, int age )
        {
            _FirstName = firstname;
            _LastName = lastname;
            _Age = age;
        }

        private string _FirstName;

        public string FirstName
        {
            get
            {
                return _FirstName;
            }
            set
            {
                _FirstName = value;
            }
        }

        private string _LastName;

        public string LastName
        {
            get
            {
                return _LastName;
            }
            set
            {
                _LastName = value;
            }
        }

        private int _Age;

        public int Age
        {
            get
            {
                return _Age;
            }
            set
            {
                _Age = value;
            }
        }
    }
}


And some code to initialize the object and deserialize it:

        static void Main( string[] args )
        {
            Person person = new Person("Joe","Soap", 20);

            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer( typeof( Person ) );

            System.IO.TextWriter stream = new System.IO.StringWriter();

            serializer.Serialize( stream, person);

            Console.WriteLine( stream.ToString() );
        }





In the many years of being a programmer I have witnessed and read many a poor attempt at achieving what I am about to show you in a few simple lines of code.

The resultant clean xml, if I may call it that, is shown below:

<Person>
  <FirstName>Joe</FirstName>
  <LastName>Soap</LastName>
  <Age>20</Age>
</Person>


The code to produce this xml is shown here:

        static void Main( string[] args )
        {
            Person person = new Person("Joe","Soap", 20);

            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer( typeof( Person ) );

            //get rid of xml header
            System.Xml.XmlWriterSettings writerSettings = new System.Xml.XmlWriterSettings();
            writerSettings.OmitXmlDeclaration = true;

            //get rid of namespaces
            System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
            ns.Add( "", "" );

            System.IO.TextWriter stream = new System.IO.StringWriter();

            using( System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create( stream, writerSettings ) )
            {
                serializer.Serialize( xmlWriter, person, ns );
            }

            Console.WriteLine( stream.ToString() );
        }

So there you have it folks. Nice and clean no funny stuff.

Until next time Happy Hacking !!


Post a Comment

Your Name:*  OR Screen Name:
Email Address:*
Comment:*
Post your Comment

    Note. All comments are sent to a moderator for approval prior to appearing on this page.

    © 2012 Saxon Systems. All rights reserved.Terms of Use  Privacy Policy  Contacts