从多个类生成XML
本文关键字:XML | 更新日期: 2023-09-27 18:28:51
我还有一个问题是如何使XML序列化整洁,但我似乎无法解决。我的配置文件如下:
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; }
}
}
理想情况下,我想做的是让这三个类(电子邮件设置、数据库和程序)中的每一个都有自己的标签,就像一样
<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>
但相反,我得到了类似的东西:
<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的建议吗?谢恩,提前谢谢。
编辑:我的序列化类。
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;
}
}
}
}
编辑2:我的新配置文件:@Craig-我正在使用这个配置文件,正如您所说,但我仍然没有得到所需的XML,显示在我的配置类之后。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Text;
命名空间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。只需将其复制并粘贴到控制台应用程序中,然后从那里获取即可。
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文件并生成XSD架构,会怎样?
例如:
xsd.exe MyXMLFile1.xml
这将生成一个XML模式文件(MyXMLFile1.xsd
)。采用模式并生成类(再次使用xsd.exe
):
xsd.exe /c MyXMLFile1.xsd
这将生成一个有保证的可序列化POCO,您可以继续使用它。类名和属性可能与当前POCO中的不匹配,但它将生成预期的XML,并从XML反序列化。
额外的好处是,今后,您只需要修改源XML文件,然后运行这两个命令来维护POCO。
希望能有所帮助。。。