.NETGURU
Collection Serialization to XML
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcs' list.


Matias Woloski
Hi!
I'm trying to serialize an object which contains a collection. this is the
code of the object Persona.

public class Persona
{
...
.
    [XmlAttribute("Nombre")]
    public string Nombre
    {
        get
            {return m_Nombre ;}
        set
            {m_Nombre = value;}
    }

    [XmlElement(typeof(Email))] <-- I don't know what to put here
    public Emails Emails
    {
        get
        {return m_Emails;}
        set
        {m_Emails = value;}
    }
}

The Emails collection is defined like this

public class Emails : IEnumerable, IEnumerator
    {
        private ArrayList m_emails = new ArrayList();
        private int intCurrent = 0;

        public Emails()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Email this[int index]
        {
            get
            { return (Email)m_emails[index]; }
        }

        public void Add (Email newEmail)
        {
            m_emails.Add(newEmail);
        }

        public int Count()
        {
            return m_emails.Count;

        }

        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)this;
        }

        public bool MoveNext()
        {
            if (intCurrent < this.Count())
            {
                intCurrent++;
                return true;
            }
            else
            {
                return false;
            }

        }

        public object Current
        {
            get { return m_emails[intCurrent-1]; }
        }

        public void Reset()
        {
            intCurrent = 0;
        }

    }

I want to generate this XML

<Persona Nombre="magoo" Apellido="magoo">
    <Emails>
        <Email mail="Click here to reveal e-mail address" ...>
        <Email mail="Click here to reveal e-mail address" ..>
    </Emails>
</Persona>

I cannot generate the <Emails> section with the <Email> elements inside.

thanks in advance,
Matias

Reply to this message...
 
    
Ryan Trudelle-Schwarz
Try the System.Xml.Serialization.XmlArray attribute.

-> -----Original Message-----
-> From: Matias Woloski [mailto:Click here to reveal e-mail address]
->
-> Hi!
-> I'm trying to serialize an object which contains a collection. this
is
-> the
-> code of the object Persona.
->
-> public class Persona
-> {
-> ...
-> .
->     [XmlAttribute("Nombre")]
->     public string Nombre
->     {
->         get
->             {return m_Nombre ;}
->         set
->             {m_Nombre = value;}
->     }
->
->     [XmlElement(typeof(Email))] <-- I don't know what to put here
->     public Emails Emails
->     {
->         get
->         {return m_Emails;}
->         set
->         {m_Emails = value;}
->     }
-> }
->
-> The Emails collection is defined like this
->
...
->
->
-> I want to generate this XML
->
-> <Persona Nombre="magoo" Apellido="magoo">
->     <Emails>
->         <Email mail="Click here to reveal e-mail address" ...>
->         <Email mail="Click here to reveal e-mail address" ..>
->     </Emails>
-> </Persona>
->
-> I cannot generate the <Emails> section with the <Email> elements
inside.
->
-> thanks in advance,
-> Matias

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
    
Matias Woloski
thanks Ryan,
I finally solved using the XmlArrayItem attribte to specify the type of the
class in the collection.

[XmlArrayItem("Email", typeof(Email))]
public Emails Emails
{...

Matias

[Original message clipped]

Reply to this message...
 
 
System.Collections.ArrayList
System.Collections.IEnumerable
System.Collections.IEnumerator
System.Xml.XmlAttribute
System.Xml.XmlElement




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification