XML Serialization

R

ranganadh

Hi all group members,


I am looking for code-sample to serialize object into XML format,
in different formats suppose think that i have one class,

[XmlRoot("MY-Employes")]
[Serializable]
public class Emp :ISerializable
{
public enum MySex
{
MALE=100,FEMALE=200,NOINFO=0
}
IFormatter = System.Runtime.Serialization.Formatters.soap
private string fname;
private string lname;
private string full_name;
private int sex=0;

[XmlElement("First_Name")]
public string MyFname
{
get
{
if (fname != null)
return fname;
else
return "NULL";
}
set
{
fname = value;
}
}

[XmlElement("Last_Name")]
public string MyLName
{
get
{
if(lname != null)
return lname;
else return "NULL";
}
set
{
lname = value;
}
}

[XmlElement("FULL_NAME")]
public string MyFullName
{
get
{
if (full_name != null)
return full_name;
else
return "NULL";
}
set
{
full_name = value;
}
}

[XmlAttribute("sex")]
public MySex MY_Sex
{
get
{
if (sex == 0)
return MySex.NOINFO;
else if (sex == 100)
return MySex.MALE;
else return MySex.FEMALE;
}
set
{
sex = 100;
}
}

void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("_Identity", this.sex);
info.AddValue("_Name", this.sex);
}


public Emp()
{
}
}


by defult it was giving,

<?xml version="1.0" encoding="utf-16"?>
<MY-Employes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" sex="MALE">
<First_Name>Ranganadh</First_Name>
<Last_Name>Kodali</Last_Name>
<FULL_NAME>NULL</FULL_NAME>
</MY-Employes>

what happen, is i am exposing my service's to the two different client
applications, one client is required in one format, and other client
is required same set of data, but in other format,


is there any procedure, will help to produce, these kind of different
format's of XML file, without changing the code file. what i am
looking for, procedure that take XSD from file system, and
serialization should take according to that specified XML definition,
so i feel it will give a grater flexibility to change my structure
later on with changing the code ..
 
M

Marc Gravell

Two options leap to mind:
1: use different classes with different xml attributes, and have some
code do the tranfer between the two.

2: use an xsl transform to translate between the two, along with a unit
test that does this and verifies that the output matches the second xsd
(may as well validate the original xml matches the first xsd too).

Marc
 
D

Duggi

Hi all group members,

    I am looking for code-sample to serialize object into XML format,
in different formats suppose think that i have one class,

[XmlRoot("MY-Employes")]
    [Serializable]
   public class Emp :ISerializable
    {
      public  enum MySex
        {
            MALE=100,FEMALE=200,NOINFO=0
        }
        IFormatter =  System.Runtime.Serialization.Formatters..soap
        private string fname;
        private string lname;
        private string full_name;
        private int  sex=0;

        [XmlElement("First_Name")]
        public string MyFname
        {
            get
            {
                if (fname != null)
                    return fname;
                else
                    return "NULL";
            }
            set
            {
                fname = value;
            }
        }

        [XmlElement("Last_Name")]
        public string MyLName
        {
            get
            {
                if(lname != null)
                    return lname;
                else return "NULL";
            }
            set
            {
                lname = value;
            }
        }

        [XmlElement("FULL_NAME")]
        public string MyFullName
        {
            get
            {
                if (full_name != null)
                    return full_name;
                else
                    return "NULL";
            }
            set
            {
                full_name = value;
            }
        }

        [XmlAttribute("sex")]
        public MySex MY_Sex
        {
            get
            {
                if (sex == 0)
                    return MySex.NOINFO;
                else if (sex == 100)
                    return MySex.MALE;
                else return MySex.FEMALE;
            }
            set
            {
                sex = 100;
            }
        }

        void ISerializable.GetObjectData(SerializationInfo info,
                                     StreamingContext context)
        {
            info.AddValue("_Identity", this.sex);
            info.AddValue("_Name", this.sex);
        }

        public Emp()
        {
        }
    }

by defult it was giving,

 <?xml version="1.0" encoding="utf-16"?>
<MY-Employes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" sex="MALE">
  <First_Name>Ranganadh</First_Name>
  <Last_Name>Kodali</Last_Name>
  <FULL_NAME>NULL</FULL_NAME>
</MY-Employes>

what happen, is i am exposing my service's to the two different client
applications, one client is required in one format, and other client
is required same set of data, but in other format,

is there any procedure, will help to produce, these kind of different
format's of XML file, without changing the code file. what i am
looking for, procedure that take XSD from file system, and
serialization should take according to that specified XML definition,
so i feel it will give a grater flexibility to change my structure
later on with changing the code ..


I think you should evaluate the Schema Providers.

Detailed information is available at http://msdn.microsoft.com/en-us/magazine/cc300797.aspx

All that you need to change is schema to change the XML format in the
serialized data. I hope this is exactly what you are looking for.

-Cnu
 

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