2

I have a simple class Student under namespace School.

namespace XmlTestApp
{
    public class Student
    {
        private string studentId;

        public string FirstName;
        public string MI;
        public string LastName;

        public Student()
        {
            //Just provided for making Serialization work as obj.GetType() needs parameterless constructor.
        }

        public Student(String studentId)
        {
            this.studentId = studentId;
        }

    }
}

Now when i serialize this, i get this as serialized xml:

<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Cad</FirstName>
  <MI>Dsart</MI>
  <LastName>dss</LastName>
</Student>

But what i want is this, basically i need the namespace prefixed to class name in xml, is this possible?

<?xml version="1.0" encoding="utf-8"?>
<XmlTestApp:Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Cad</FirstName>
  <MI>Dsart</MI>
  <LastName>dss</LastName>
</Student>

Here's my serialization code:

Student s = new Student("2");
            s.FirstName = "Cad";
            s.LastName = "dss";
            s.MI = "Dsart";

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(s.GetType());

            TextWriter txtW=new StreamWriter(Server.MapPath("~/XMLFile1.xml"));
            x.Serialize(txtW,s);
Charu
  • 2,679
  • 6
  • 35
  • 52
  • 1
    http://stackoverflow.com/questions/1254544/how-do-i-specify-xml-serialization-attributes-to-support-namespace-prefixes-duri – NickD Jun 18 '12 at 19:01

3 Answers3

2

EDIT: Short answer is still yes. The proper attribute is actually the XmlType attribute. In addition, you will need to specify a namespace, and then in the serialization code you will need to specify aliases for the namespaces that will be used to qualitfy elements.

namespace XmlTestApp
{
    [XmlRoot(Namespace="xmltestapp", TypeName="Student")]
    public class Student
    {
        private string studentId;

        public string FirstName;
        public string MI;
        public string LastName;

        public Student()
        {
            //Just provided for making Serialization work as obj.GetType() needs parameterless constructor.
        }

        public Student(String studentId)
        {
            this.studentId = studentId;
        }

    }
}

...

        Student s = new Student("2");
        s.FirstName = "Cad";
        s.LastName = "dss";
        s.MI = "Dsart";

        System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(s.GetType());

        System.Xml.Serialization.XmlSerializationNamespaces ns = new System.Xml.Serialization.XmlSerializationNamespaces();

        ns.Add("XmlTestApp", "xmltestapp");

        TextWriter txtW=new StreamWriter(Server.MapPath("~/XMLFile1.xml"));
        x.Serialize(txtW,s, ns); //add the namespace provider to the Serialize method

You may have to play around with the setting up of the namespace to ensure it still uses the XSD/XSI from W3.org, but this should get you on the right track.

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • I am getting this Cad Dsart dss – Charu Jun 18 '12 at 19:13
  • Hmm. The colon is being escaped into its hex value. The colon is valid for XML elements (Production 4 and 5), but it's usually used to separate a namespace shortcut from the actual type. Edits to follow. – KeithS Jun 18 '12 at 19:57
0

Another way how to achieve it, is to write your xml - than use tool in visual studio - xml to xsd. If you have xsd, you can generate serializeable classes with xsdToCode

Martin Ch
  • 1,337
  • 4
  • 21
  • 42
0

An elegant solution would be to use XmlSerializerNamespaces to declare your namespace and then pass that into the XmlSerializer

See XML Serialization and namespace prefixes

Community
  • 1
  • 1
robbymurphy
  • 741
  • 2
  • 5
  • 16