Serializing objects to xml in C#

12,578

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.

Share:
12,578
Charu
Author by

Charu

Updated on June 15, 2022

Comments

  • Charu
    Charu almost 2 years

    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
    Charu almost 12 years
    I am getting this <XmlTestApp_x003A_Student xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema"> <FirstName>Cad</FirstName> <MI>Dsart</MI> <LastName>dss</LastName> </XmlTestApp_x003A_Student>
  • KeithS
    KeithS almost 12 years
    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.