Charu
Charu

Reputation: 2749

Serializing objects to xml in C#

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);

Upvotes: 2

Views: 12610

Answers (3)

KeithS
KeithS

Reputation: 71565

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.

Upvotes: 2

robbymurphy
robbymurphy

Reputation: 741

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

Upvotes: 0

Martin Ch
Martin Ch

Reputation: 1367

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

Upvotes: 0

Related Questions