从多个类生成 XML? [英] Generating XML from multiple classes?

查看:25
本文介绍了从多个类生成 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有另一个问题是关于让我的 XML 序列化整洁,我似乎无法正确回答.我的配置文件如下:

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>

很抱歉打扰了,但现在这让我很困惑,我确定这里缺少一些基本逻辑..谁能给我一些关于如何以我指定的格式获取此 XML 的指示以上?提前致谢,谢恩.

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.

我的序列化类.

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


}

}

我的新配置文件:@Craig - 我正在使用这个配置文件,就像你说的,但我仍然没有得到所需的 XML,显示在我的配置类之后.

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;

命名空间 SMCProcessMonitor{[序列化()]

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

但我仍然得到:

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

推荐答案

这会给你想要的输出:

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

<小时>

这是一个控制台应用程序,它将对象序列化为文件并生成您正在寻找的确切 XML.只需将其复制并粘贴到控制台应用程序中,然后从那里获取即可.


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
}

这篇关于从多个类生成 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆