Elimnating namespace in XML

D

Daniel Strigl

Hi Barry!
I need generate an XML message without the namespace using XmlSerializer,
have tried XmlRootAttribute without much help

I need to elimate xmlns:xsd=......

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

The following sample shows how it works:

public class Student
{
public string Name;
public int ID;
}

public class Program
{
static void Main(string[] args)
{
// Create an instance of the object to serialize
Student student = new Student();
student.Name = "John Doe";
student.ID = 1234;

// Create our own namespaces for the output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

// Add an empty namespace and empty value
ns.Add("", "");

// Create the serializer
XmlSerializer serializer =
new XmlSerializer(typeof(Student));

// Serialize the object with our own namespaces
TextWriter writer = new StreamWriter("student.xml");
serializer.Serialize(writer, student, ns);
writer.Close();
}
}

More info can be found at:
http://www.csharper.net/blog/serializing_without_the_namespace__xmlns__xmlns_xsd__xmlns_xsi_.aspx.

Regards,
Daniel.
 

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