tags:

views:

998

answers:

6

Can someone please help me, I have this xml snippet

<?xml version="1.0" encoding="utf-8" ?>
<EmailConfiguration>
  <DataBoxID>123</DataBoxID>
  <DefaultSendToAddressCollection>
     <EmailAddress>[email protected]</EmailAddress>
  </DefaultSendToAddressCollection>
</EmailConfiguration>

I want to create a corressponding c# class from this. Before you say - "Just use xsd.exe", the output from Xsd cannot be serialized and deserialized correct, because it generates the class using partial classes.

Please can you tell me how to create this class.... here is the approach I took, but it doesn't work.

public class EmailConfiguration
{
    private string dataBoxID;

    public string DataBoxID
    {
        get { return dataBoxID; }
        set { dataBoxID = value; }
    }

    private DefaultSendToAddressCollectionClass defaultSendToAddressCollection;

    public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection
    {
        get { return defaultSendToAddressCollection; }
        set { defaultSendToAddressCollection = value; }
    }
}

And here is the class declaration for the subclass

public class DefaultSendToAddressCollectionClass
{
    private string[] emailAddress;
    public string[] EmailAddress
    {
        get { return emailAddress; }
        set { emailAddress = value; }
    } 
}
A: 

XML serialization requires attributes. The way I've usually done it is to flag the class itself with [Serializable] and [XmlRoot], then mark up public properties with either [XmlElement], [XmlAttribute] or [NoSerialize].

What specific problem are you having?

Steven Sudit
+1  A: 

Did you use VS2008's XSD?

Here's the output I got:

c:>xsd email.xml
Writing file 'c:\email.xsd'

c:>xsd email.xsd /c /edb
Writing file 'c:\email.cs'

Generates serializable output:

[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 : object,  System.ComponentModel.INotifyPropertyChanged {

private string dataBoxIDField;

private EmailConfigurationDefaultSendToAddressCollection[] defaultSendToAddressCollectionField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string DataBoxID {
    get {
        return this.dataBoxIDField;
    }
    set {
        this.dataBoxIDField = value;
        this.RaisePropertyChanged("DataBoxID");
    }
}
Shane Cusson
A: 

XSD.EXE is the tool that produces classes specifically for the purpose of XML Serialization. If it produces partial classes, that's because they work for XML Serialization. That's not what your problem is.

Try using XSD.EXE and serializing / deserializing. If you get an exception again, then please catch it and then post the results of ex.ToString().

John Saunders
+1  A: 

Using .NET 3.5:

[XmlRoot]
public class EmailConfiguration
{
    [XmlElement]
    public string DataBoxID { get; set; }

    [XmlElement]
    public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection { get; set; }
}

public class DefaultSendToAddressCollectionClass
{
    [XmlElement]
    public string[] EmailAddress { get; set; }
}
Zanoni
A: 

This class will serialize the way you want. I changed your custom collection to a List and used the XmlArrayItem attribute to specify how each email address would be serialized. There are many such attributes to help you fine tune the serialization process.

[Serializable]
public class EmailConfiguration {
    private string dataBoxID;
    public string DataBoxID {
        get { return dataBoxID; }
        set { dataBoxID = value; }
    }

    private List<string> defaultSendToAddressCollection;

    [XmlArrayItem("EmailAddress")]
    public List<string> DefaultSendToAddressCollection {
        get { return defaultSendToAddressCollection; }
        set { defaultSendToAddressCollection = value; }
    }

    public EmailConfiguration() {
        DefaultSendToAddressCollection = new List<string>();
    }
}
Chris Dunaway
+1  A: 

Bare minimum working... looks like you are only required to add one attribute.

public class EmailConfiguration
{
    public string DataBoxID { get; set; }
    public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection { get; set; }
}

public class DefaultSendToAddressCollectionClass
{
    [XmlElement]
    public string[] EmailAddress { get; set; }
}
Matthew Whited