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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

classes to 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;}
        }
        
    }
 
Have you tried using xsd to generate the classes based on the schema / xml document that you require ?

or use your current class, and add the IXmlSerializable Interface.

Then you can override the WriteXml serlaliser method.

hth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top