Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ood with serialization

Status
Not open for further replies.

jrenae

Programmer
Jan 18, 2006
142
US
I'm trying to construct some classes such that I will get the desired result when serializing:

Code:
<?xml version="1.0" encoding="utf-16"?>
<LDPDetails xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]
  <DistributionType>SMTP</DistributionType>
  <DistributeTo>
    <EmailList>
     <EmailAddressTo>jean.stiles@reply.com</EmailAddressTo>
     <EmailAddressTo>jeanstiles@comcast.net</EmailAddressTo>
     <EmailAddressFrom>leads@chron.com</EmailAddressFrom>
    </EmailList>
  </DistributeTo>
</LDPDetails>
However this is my output based on the classes below. Notice that I want to suppress the parent <EmailAddressTo> element tag and only include the child <EmailAddressTo> nodes that have the values. Any suggestions on how to construct the EmailList class? Thanks.
Code:
<?xml version="1.0" encoding="utf-16"?>
<LDPDetails xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]
  <DistributionType>SMTP</DistributionType>
  <DistributeTo>
    <EmailList>
     <EmailAddressTo> --want to suppress
           <EmailAddressTo>jean.stiles@reply.com</EmailAddressTo>
           <EmailAddressTo>jeanstiles@comcast.net</EmailAddressTo>
      </EmailAddressTo>--want to suppress
      <EmailAddressFrom>leads@chron.com</EmailAddressFrom>
    </EmailList>
  </DistributeTo>
</LDPDetails>

classes:
Code:
    public class LDPDetails
    {
        private String _distributionType;
        private DistributeTo _distributeTo;
        
        
        public String DistributionType
        {
            get {return this._distributionType;}
            set {this._distributionType = value;}
        }
        public DistributeTo DistributeTo
        {
            get {return this._distributeTo;}
            set {this._distributeTo = value;}
        }
    }
    
    public class DistributeTo
    {
        private EmailList _emailList;
        private String _soapAction;
        private String _contentType;
        private String _url;
        
        public EmailList EmailList
        {
            get {return this._emailList;}
            set {this._emailList = value;}
        }
        public String SoapAction
        {
            get {return this._soapAction;}
            set {this._soapAction = value;}
        }
        public String ContentType
        {
            get {return this._contentType;}
            set {this._contentType = value;}
        }
        public String URL
        {
            get {return this._url;}
            set {this._url = value;}
        }
        
    }
    
    public class EmailList
    {
        private String[] _emailAddressTo;
        private String _emailAddressFrom;

       [XmlArray(ElementName=null)]
       [XmlArrayItem(Type = typeof(string), ElementName = "EmailAddressTo")]
        public String[] EmailAddressTo
        {
            get
            {
                return this._emailAddressTo;
            }
            set
            {
                this._emailAddressTo = value;
            }
        }
        

        public String EmailAddressFrom
        {
            get {return this._emailAddressFrom;}
            set {this._emailAddressFrom = value;}
        }
        
    }
 
this looks like C#, better to ask in forum732.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I don't think it's a good idea to design your objects thinking in serialization, I think it's better to adapt the serailization to your object design.

In Java there is a keyword (transient or volatile, I can't remember now) that will prevent some fields from being serialized.

Is there something similar in C#?

Cheers,
Dian
 
Thank you Dian. The reason I had to design it around serialization is that we were already storing the xml for this new class in a sql server xml datatype column (we were just building the xml manually thru code), and we didn't want to change any of our existing code that accesses it via other services. So I wanted to make sure that when I serialized this new class it went into the db in the exact form as already exists.

I did find the solution which was very easy by taking the xml and creating a class using the xsd.exe utility.

All I needed was this at the top of my EmailAddressTo property:
Code:
[System.Xml.Serialization.XmlElementAttribute("EmailAddressTo")]
heres the complete class:
Code:
    public class EmailList
    {
        private String[] _emailAddressTo;
        private String _emailAddressFrom;

        [System.Xml.Serialization.XmlElementAttribute("EmailAddressTo")]
        public String[] EmailAddressTo
        {
            get
            {
                return this._emailAddressTo;
            }
            set
            {
                this._emailAddressTo = value;
            }
        }

        public String EmailAddressFrom
        {
            get {return this._emailAddressFrom;}
            set {this._emailAddressFrom = value;}
        }
    }

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top