我還有一個關於讓我的XML序列化整齊的問題,我似乎無法得到正確的答案。我的配置文件如下:從多個類生成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>
對不起,是這樣的麻煩,但現在這是在做我的螺母和IM肯定有我的思念here..can一些基本的邏輯有人給我了一些p-關於如何以上面指定的格式獲取這個XML? 謝謝,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;
}
}
}
}
編輯2: 我的新的配置文件: @Craig - 我使用這個配置文件,它是像你說的,但即時通訊仍然沒有得到想要的XML,後顯示我的配置類。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Text;
命名空間SMCProcessMonitor { [序列化()]
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>
你能寫出你用於序列化的代碼段嗎? – daryal 2012-03-23 12:20:57
這是我的序列化代碼。請注意,由於將變量移動到其各自的類,因此在寫入時告訴我「對象引用未設置爲對象的實例」時,現在遇到錯誤。 – 2012-03-23 13:56:45
@Shane'Shamus'Coulter使用你添加到你的問題中的序列化代碼,將使用我提供的'class'修改來輸出你想要的XML。爲了測試它,你可以逐字從我的答案中複製這些類,填充一個'Config'對象並通過你發佈的序列化代碼運行它。你需要改變的唯一事情就是你的財產獲得者和制定者在適用的情況下。 – Craig 2012-03-23 14:14:02