gpt4 book ai didi

c# - 将应用程序设置导出到 XML

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:01 27 4
gpt4 key购买 nike

以下情况:

我将一些值保存到我的应用程序设置中 (Properties.Settings)在此设置中,我还保存了一些对象,其中存储了该对象的属性。

现在我想实现一个将我的设置导出到一个xml文件中的功能。

对于整数值和字符串,我是这样做的:

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();

好的..我希望到目前为止这是正确的方法。

现在我需要将我的对象存储到这个文件中。

我的想法是使用 XmlSerializer 类。但不幸的是,我完全不知道如何使用它来将对象与一个 XML 文件中的其他值组合起来

此外,这是我要写入 XML 文件的类的代码: http://pastebin.com/PmB4tM7b

最佳答案

为了对您的对象使用 XML 序列化,您需要使用预定义的 serialization attributes 修饰您的类或包含您的类的数据结构。 :

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

可以这样使用:

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

此行创建以下 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>

从 XML 文件中检索对象:

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

关于c# - 将应用程序设置导出到 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18687599/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com