XML to C# Class Question

2020-02-10 06:50发布

Can someone please help me, I have this xml snippet

<?xml version="1.0" encoding="utf-8" ?>
<EmailConfiguration>
  <DataBoxID>123</DataBoxID>
  <DefaultSendToAddressCollection>
     <EmailAddress>email@whereEver.com</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; }
    } 
}

标签: c# xml class
7条回答
smile是对你的礼貌
2楼-- · 2020-02-10 07:19

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?

查看更多
登录 后发表回答