Re: Elimnating namespace in XML

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.

--
Daniel Strigl [daniel.strigl@xxxxxxxxxxxxxxxxxx, remove 'takethisout.']
Web: http://www.hh-system.com/danielstrigl
Blog: http://geekswithblogs.net/dastblog
.



Relevant Pages

  • Re: Serialization and Xml mapping
    ... public class Deserializer ... /// Represents a single manager ... private string strFirstName_m; ... serialize / deserialize informations stored in the XML "as is". ...
    (microsoft.public.dotnet.framework)
  • Retriving Inherited Objects in an Array
    ... public class Application ... When the Jade web service returns these paramaters. ... <Student xmlns:xsd="http://www.w3.org/2001/XMLSchema " ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Serializing a class with a string[] array
    ... A string array should serialize fine. ... > public class AppStart ... > public class MyClass ...
    (microsoft.public.dotnet.xml)
  • Re: Serialization and inheritance
    ... That's a pity. ... Forces me to move my student class to the shared assembly, ... >> I'm getting an error when I try to serialize a class in my project. ... >> public int PersonData; ...
    (microsoft.public.dotnet.languages.csharp)
  • Serializing a list of different object types.
    ... I have my data stored in a set of classes (or structs, doesn't matter), ... which I am trying to serialize. ... string A1; ... public class Container ...
    (microsoft.public.dotnet.languages.csharp)