Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:35:14+00:00 2026-05-31T17:35:14+00:00

i have another question on getting my XML serialization neat, which i can’t seem

  • 0

i have another question on getting my XML serialization neat, which i can’t seem to get right. my config file is as follows:

namespace SMCProcessMonitor
{
[Serializable()]
[XmlRoot("Email-Settings")] 
     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>();
         public string serialId;
     }

     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; }

    }
         [Serializable()]
    public class Programs
{
        [XmlElement("Filename")] public string mFileName { get; set; }
        [XmlElement("Filepath")]public string mFilePath { get; set; }
}

         public class Database
         {
             public string mSerial { get; set; }
         }
         }

Ideally what i want to do is have each of these three classes (email settings, database and programs) have their own tags, like so

<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<email-settings>
  <Recipient>sadh</Recipient>
  <Server-port>23</Server-port>
  <Username>lkms</Username>
  <Password>kmkdvm</Password>
</email-settings>
<Program>
  <Filename>MerlinAlarm.exe</Filename>
  <Filepath>D:\Merlin\Initsys\Merlin\Bin\MerlinAlarm.exe</Filepath>
</Program>
<database-settings>
  <serialId>1</serialId>
</database-settings>
</Config>

But instead i get something that resembles this:

 <Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Recipient>blah</Recipient>
 <Server-port>1111</Server-port>
 <Username>blah</Username>
 <Password>blah</Password>
 <Program>
 <Filename>chrome.exe</Filename>
 <Filepath>
 C:\Users\Shane\AppData\Local\Google\Chrome\Application\chrome.exe
 </Filepath>
 </Program>
 <serialId>1234</serialId>
 </Config>

Sorry to be such a bother, but this is doing my nut in now and im sure there’s some fundamental logic i’m missing here..can anyone give me some pointers as to how to get this XML in the format i specified above?
Thanks in advance, Shane.

Edit: My serialization class.

 namespace SMCProcessMonitor
{
public class ShanesXMLserializer
{
    private string  mFileAndPath;
    public Config   mConfigurations = null;
    public Config mConfigurationsProgram = null;



    public ShanesXMLserializer(string inFileAndPath)
    {
        mFileAndPath    = inFileAndPath;
        mConfigurations = new Config();

    }

    public bool Write()
    {
        try
        {
            XmlSerializer x = new XmlSerializer(mConfigurations.GetType());
            StreamWriter writer = new StreamWriter(mFileAndPath);
            x.Serialize(writer, mConfigurations);
            writer.Close();
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception found while writing: " + ex.Message);
        };

        return false;
    }

    public bool Read()
    {
        try
        {
            XmlSerializer x = new XmlSerializer(typeof(Config));
            StreamReader reader = new StreamReader(mFileAndPath);
            mConfigurations = (Config)x.Deserialize(reader);
            reader.Close();
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception found while reading: " + ex.Message);
        };

        return false;
    }

    public Config GetConfigEmail
    {
        get
        {
            return mConfigurations;
        }
    }


}

}

Edit 2:
My new config file:
@Craig – I’m using this config file, which is like you said but im still not getting the desired XML, shown after my config class.

           using System;
           using System.Collections.Generic;
           using System.Linq;
           using System.Xml.Serialization;
           using System.Text;

namespace SMCProcessMonitor
{
[Serializable()]

     public class Config
{     

         public string recipient;
         public int serverport;
         public string username;
         public string password;
         public List<Programs> mPrograms = new List<Programs>();
         public string serialId;
    [XmlElement("email-settings")]
         public Email Email { get; set; }
         public Programs Programs { get; set; }
     [XmlElement("database-settings")]
         public Database Database { get; set; }


     }

     public class Email
    {
         [XmlElement("Recipient")]
         public string Recipient { get; set; }
            [XmlElement("Server-port")]
         public int ServerPort { get; set; }
         [XmlElement("Username")]
         public string Username { get; set; }
         [XmlElement("Password")]
         public string Password { get; set; }

    }
         [Serializable()]
    public class Programs
    {
        [XmlElement("Filename")] public string mFileName { get; set; }
        [XmlElement("Filepath")]public string mFilePath { get; set; }
    }

    public class Database
    {
        [XmlElement("SerialID")]
        public string mSerial { get; set; }
    }
    }

But i am still getting:

  <Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <recipient>shane</recipient>
  <serverport>23</serverport>
  <username>oid</username>
  <password>jidj</password>
  <mPrograms/>
  </Config>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:35:15+00:00Added an answer on May 31, 2026 at 5:35 pm

    This will give you the desired output:

    public class Config
    {
        [XmlElement("email-settings")]
        public Email Email { get; set; }
    
        public Program Program { get; set; }
    
        [XmlElement("database-settings")]
        public Database Database { get; set; }
    }
    
    public class Email
    {
        public string Recipient { get; set; }
    
        [XmlElement("Server-port")]
        public int ServerPort { get; set; }
    
        public string Username { get; set; }
        public string Password { get; set; }
    }
    
    public class Program
    {
        public string Filename { get; set; }
    
        public string Filepath { get; set; }
    }
    
    public class Database
    {
        public string serialId { get; set; }
    }
    

    Here’s a console application which will serialize an object to a file and produce the exact XML you are looking for. Just copy and paste it into a console application and take it from there.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var config = new Config
                {
                    Email = new Email
                    {
                        Recipient = "sadh",
                        ServerPort = 23,
                        Username = "lkms",
                        Password = "kmkdvm"
                    },
                    Program = new Programs
                    {
                        Filename = "MerlinAlarm.exe",
                        Filepath = @"D:\Merlin\Initsys\Merlin\Bin\MerlinAlarm.exe"
                    },
                    Database = new Database
                    {
                        serialId = "1"
                    }
                };
    
                XmlSerializer serializer = new XmlSerializer(typeof(Config));
    
                var textWriter = new StreamWriter(@"C:\config.xml");
                serializer.Serialize(textWriter, config);
                textWriter.Close();
    
                Console.Read();
            }
        }
    
        #region [Classes]
    
        public class Config
        {
            [XmlElement("email-settings")]
            public Email Email { get; set; }
    
            public Programs Program { get; set; }
    
            [XmlElement("database-settings")]
            public Database Database { get; set; }
        }
    
        public class Email
        {
            public string Recipient { get; set; }
    
            [XmlElement("Server-port")]
            public int ServerPort { get; set; }
    
            public string Username { get; set; }
            public string Password { get; set; }
        }
    
        public class Programs
        {
            public string Filename { get; set; }
    
            public string Filepath { get; set; }
        }
    
        public class Database
        {
            public string serialId { get; set; }
        }
    
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently posted another question which straight away users pointed me in the
I have a question very similiar to another question: Get name of property as
I have another beginner's question that hopefully someone can help with. I'm trying to
Okay, so I have another question on a prolog homework problem I am struggling
i have another (probably unanswered) question about map views. I have a map view
Another question related to this one . I have a List<SortableObjects> that is the
This question is kind of related to another question but I have a specific
Per a great answer from another question I have begun mounting global resources (css/js/images)
As described in another question , I have a set of Model objects and
This question is related to another question I asked Basically, I have 2 horizontally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.