简体   繁体   English

将应用程序设置导出到XML

[英]Export application settings to XML

Following situation: 以下情况:

I have some values saved into my application settings (Properties.Settings) In this settings i have also saved some objects, where the properties of this objects are stored. 我将一些值保存到应用程序设置(Properties.Settings)中。在此设置中,我还保存了一些对象,这些对象的属性存储在该对象中。

Now i want to implement a function to export my settings into a xml file. 现在,我想实现一个将设置导出到xml文件的功能。

for integer values and strings i did it this way: 对于整数值和字符串,我这样做:

XmlWriter writer = XmlWriter.Create(path, settings);
writer.WriteStartDocument();
writer.WriteElementString("ServerIP", Settings.Default.ServerIP);
writer.WriteElementString("ServerPort", Settings.Default.ServerPort.ToString());
writer.WriteEndDocument();
writer.Flush();
writer.Close();

Ok.. i hope this is the correct way until now. 好吧..我希望这是到目前为止的正确方法。

Now i need to store my objects into this file. 现在我需要将对象存储到该文件中。

My idea was to use the XmlSerializer class. 我的想法是使用XmlSerializer类。 But unfortunately i have absolutely no clue how to use it to get the objects combined with the other values in one XML file 但是不幸的是,我完全不知道如何使用它来将对象与一个XML文件中的其他值组合在一起

in addition here is the code of the class the i want to write to the XML file: http://pastebin.com/PmB4tM7b 另外,这里是我要写入XML文件的类的代码: http : //pastebin.com/PmB4tM7b

In order to use the XML serialization for your objects, you need to decorate your classes or data structures, that hold your classes, with the predefined serialization attributes : 为了对您的对象使用XML序列化,您需要使用预定义的序列化属性来修饰保存类的类或数据结构:

// block of settings like 
// <Service>
//  <Name>Service1</Name>
//  <Description>Starts the service 1</Description>
// </Service>
public class SettingsService
{
    // will be a node in the XML file
    [XmlElement(ElementName="Name")]
    public string Name { get; set; }
    // will be a node too
    [XmlElement(ElementName="Description")]
    public string Description { get; set; }
}

// holds a list of services
// <Services>
//  <Service>...</Service>
//  <Service>...</Service>
// </Services>
public class ServicesSettings
{
    // list of services
    [XmlArray(ElementName="SettingsServices")]
    public List<SettingsService> Services { get; set; }
    // single value!
    [XmlElement(ElementName="SettingsPort")]
    public int PortNumber { get; set; }
}

// serializes the objects to XML file
public void SerializeModels(string filename, ServicesSettings settings)
{
    var xmls = new XmlSerializer(settings.GetType());
    var writer = new StreamWriter(filename);
    xmls.Serialize(writer, settings);
    writer.Close();
}

// retrieves the objects from an XML file
public ServicesSettings DeserializeModels(string filename)
{
    var fs = new FileStream(filename, FileMode.Open);
    var xmls = new XmlSerializer(typeof(WindowsServicesControllerSettings));
    return (WindowsServicesControllerSettings) xmls.Deserialize(fs);
}

Can be used like this: 可以这样使用:

var service1 = new SettingsService { Name = "Service1", Description = "Blah blah" };
var service2 = new SettingsService { Name = "Service2", Description = "Blah blah blup" };
var services = new List<SettingsService> { service1, service2 };
var settings = new ServicesSettings
{ 
    Services = services,
    PortNumber = 1234
};

this.SerializeModels(@"d:\temp\settings.xml", settings);

This line creates the following XML file: 此行创建以下XML文件:

<?xml version="1.0" encoding="utf-8"?>
<ServicesSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SettingsServices>
    <SettingsService>
      <Name>Service1</Name>
      <Description>Blah blah</Description>
    </SettingsService>
    <SettingsService>
      <Name>Service2</Name>
      <Description>Blah blah blup</Description>
    </SettingsService>
  </SettingsServices>
  <SettingsPort>1234</SettingsPort>
</ServicesSettings>

Retrieving the objects back from the XML file: 从XML文件检索对象:

var settings = this.DeserializeModels(@"d:\temp\settings.xml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM