views:

1102

answers:

5

I have this xml snippet as part of a xml file that will be deserialized to a c# object

<EmailConfiguration>
<SendTo>
 <Address>[email protected]</Address>
 <Address>also send to</Address>
</SendTo>
<CC>
 <Address>CC</Address>
</CC>
<BCC>
 <Address>BCC</Address>
</BCC>
</EmailConfiguration>

Notice the SendToAddress is a collection of Addresses

<Address>[email protected]</Address>
<Address>also send to</Address>

My Class currently looks like this

public class SendTo
{
    public string Address
    {
        get;
        set;
    }
}

This is good for 1 address, how do I change the class so that it can handle more than 1 address.

I tried

public class SendTo
{
    public string[] Address
    {
        get;
        set;
    }
}

But with this, nothing gets populated when the xml is deserialized to a class.

Update

tried...also without success....

public class SendTo
{
    public List<string> Address
    {
        get;
        set;
    }
}

Here is the entire class

namespace myConfiguration
   {
public class myConfiguration
{
    private string myID;


    public string MyID
    {
        get { return myID; }
        set { myID = value; }
    }

    public Locale Locale
    {
        get;
        set;
    }

    public EmailConfiguration EmailConfiguration
    {
        get;
        set;
    }

}

public class Locale
{
    public string LocaleName
    {
        get;
        set;
    }
    public string LocaleCode
    {
        get;
        set;
    }
}

public class EmailConfiguration
{
    public SendTo SendTo
    {
        set;
        get;
    }
    public SendTo CC
    {
        set;
        get;
    }
    public SendTo BCC
    {
        set;
        get;
    }
}

public class SendTo
{
    public List<string> Address
    {
        get;
        set;
    }
}

}

Here is the XML that I am deserializing from ...

 <?xml version="1.0" encoding="utf-8" ?>
 <MyConfiguration>
 <MyID>vpfaewb</MyID>
 <Locale>
    <LocaleName>cs-CZ</LocaleName>
 <LocaleCode>1029</LocaleCode>
 </Locale>
<EmailConfiguration>
<SendTo>
 <Address>my email address</Address>
 <Address>also send to</Address>
</SendTo>
<CC>
 <Address>CC</Address>
</CC>
<BCC>
 <Address>BCC</Address>
</BCC>
  </EmailConfiguration>
</MyConfiguration>
A: 

Use A List<Address> collection; it's serializabe

Janie
Question updated....
JL
Arrays are also serializable.
Robert Harvey
I decided to go for the List, thanks for the tip. It just looks cleaner than an array, and I am sure easier on the memory
JL
A: 

Might not be the best type to use, but an ArrayList is also serializable and worth a shot.

AcousticBoom
+1  A: 

If you run your test XML file through the xsd.exe tool by Microsoft twice, first turning the XML into a XSD (xsd.exe yourfile.xml --> yourfile.xsd), and then generating a C# class from that XSD (xsd.exe /c yourfile.xsd --> yourfile.cs) that can deserialize that XML content, you'll end up with something like this (long and not very pretty.....)

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4016
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class EmailConfiguration {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("BCC", typeof(EmailConfigurationBCC), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("CC", typeof(EmailConfigurationCC), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("SendTo", typeof(EmailConfigurationSendTo), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationBCC {

    private string addressField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationCC {

    private string addressField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationSendTo {

    private EmailConfigurationSendToAddress[] addressField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Address", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public EmailConfigurationSendToAddress[] Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmailConfigurationSendToAddress {

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

I would think this will work - it's not nice, but it'll work. You should be able to convert your complete XML file into a serializable / deserializable C# class that way. Try it!

Marc

marc_s
Thanks Marc for your answer, I don't want to knock the XSD tool, but I did go that route first, and it didn't create the object correctly, even after I manually hacked the XSD, so eventually I decided to do it the manual route.
JL
no problem - sometimes it works fine, in other cases not so much.... but it might give you an idea what attributes and such you're missing.
marc_s
+1  A: 

Try this:

[XmlArrayItem("Address", IsNullable=false)]
public string[] SendTo
{
    get
    {
        return this.sendToField;
    }
    set
    {
        this.sendToField = value;
    }
}
DannyAsher
You're done it! Thanks Danny :) That works, can you please explain why this works?
JL
from http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayitemattribute.aspx:The XmlArrayItemAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object.You can apply the XmlArrayItemAttribute to any public read/write member that returns an array, or provides access to one. For example, a field that returns an array of objects, a collection, an ArrayList, or any class that implements the IEnumerable interface.
DannyAsher
Very useful to know!!! Thanks again Mate
JL
A: 

Check out Xml Serialization tip 2 for details about serializing a List collection.

Ali B