Kestami
Kestami

Reputation: 2073

Set XML serialziation resulting doc root

I've got another problem (which might not be an issue in terms of coding problems) but more of principle..been bugging me for a while. I have this c# class, as follows:

namespace SMCProcessMonitor
{
    public class Config
    {     
        [XmlElement("Recipient")]
        public string recipient;

        [XmlElement("Server-port")]
        public int serverport;

        [XmlElement("Username")]
        public string username;

        [XmlElement("Password")]
        public string password;

        [XmlElement("Program")]
        public List<Programs> mPrograms = new List<Programs>();

        [Serializable]
        [XmlRoot("Email-Config")]
        public class Email
        {
            public string Recipient
            {
                get
                {
                    return SMCProcessMonitor.ConfigManager.mConfigurations.recipient;
                }
                set
                {
                    SMCProcessMonitor.ConfigManager.mConfigurations.recipient = value;
                }
             }

             public int ServerPort
             {
                 get
                 {
                     return SMCProcessMonitor.ConfigManager.mConfigurations.serverport;
                 }
                 set
                 {
                     SMCProcessMonitor.ConfigManager.mConfigurations.serverport = value;
                 }
             }
             public string Username
             {
                 get
                 {
                     return SMCProcessMonitor.ConfigManager.mConfigurations.username;
                 }
                 set
                 {
                     SMCProcessMonitor.ConfigManager.mConfigurations.username = value;
                 }
             }

        public string Password { get; set; }
    }
}

I can serialize this almost fine. (i recently changed simple get; set; to the full-works as seen above, but when serialising i get something like this;

<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Recipient>sd</Recipient>
    <Server-port>1234</Server-port>
    <Username>dk</Username>
    <Password>kdkdk</Password>
</Config>

Basically I want to wrap these 4 tags in an "email-settings" tag.

Upvotes: 0

Views: 91

Answers (3)

asawyer
asawyer

Reputation: 17808

There are attributes to control aspects of xml serialization like this, see Controlling XML Serialization Using Attributes.

I think the one you want specifically is XmlRootAttribute.

Upvotes: 1

mgnoonan
mgnoonan

Reputation: 7200

Add the Serializable() and XmlRoot attributes up to the base class:

[Serializable()] 
[XmlRoot("Email-Settings")] 
public class Config 

Upvotes: 2

Brian Driscoll
Brian Driscoll

Reputation: 19635

You'll need to create an EmailSettings class that contains those 4 properties, and then make an instance of the EmailSettings class a member of your Config class.

Upvotes: 0

Related Questions