序列化/反序列化到/从多个XML配置文件 [英] Serialize/deserialize to/from multiple XML config files

查看:258
本文介绍了序列化/反序列化到/从多个XML配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET中有一组自定义XML配置管理类。



我的应用程序有两个配置范围:



用户:该设置适用于用户,无论她登录的帐户如何。存储在用户目录中的XML配置文件中。



用户/帐户:该设置适用于登录到特定帐户的用户。



这两个XML文档具有相同的结构,可以手动修改。当应用程序启动时,我从用户的配置文件和用户/帐户目录中的XML配置读取XML配置。然后,我将两个XML文档合并为一个XDocument,然后反序列化为XDocument的部分之后建模的对象的对象。如果存在用户/帐户级别设置,则它应覆盖用户级别设置。示例:



用户文件

 < FileSettings> 
< DownloadPath> C:\downloads< / DownloadPath>
< UploadPath> C:\uploads< / UploadPath>
< / FileSettings>

帐户XYZ的用户/帐户文件:

 < FileSettings> 
< DownloadPath> C:\\\
ewlocation\xyz\mystuff< / DownloadPath>
< / FileSettings>

合并后的结果

 < FileSettings> 
< DownloadPath> C:\\\
ewlocation\xyz\mystuff< / DownloadPath>
< UploadPath> C:\uploads< / UploadPath>
< / FileSettings>

在上面的例子中,具有2个属性的FileSettings对象 - DownloadPath和UploadPath将从。



这是我的问题:我不知道DownloadPath和UploadPath是从哪个配置文件(即作用域)。因此,当对象需要再次序列化时,它不知道哪些属性在哪个文件中。



问题:在基于属性的基础上存储源的最佳方法是,

解决方案

/ div>

合并后,是否可以追加每个属性的值来源?

 < FileSettings> 
< DownloadPath> C:\\\
ewlocation\xyz\mystuff< / DownloadPath>
< UploadPath> C:\uploads< / UploadPath>
< DownloadPathSrc> xyz< / DownloadPathSrc>
< DownloadPathSrc> user< / DownloadPathSrc>
< / FileSettings>

如果是这样,两个src属性可以反序列化到您的FileSettings对象
序列化之前再次为文件创建一个全新的FileSetting对象,为每个源WITHOUT设置* src属性,默认情况下,空字符串不会被XmlSerializer序列化。使用src属性,您知道要序列化的对象所在的文件。



EDIT,
如果要添加 src 作为元素的属性。
这实际上是我的初步想法,但是,你需要为每个属性的create类。例如对于属性DownloadPath在FileSetting类中

  [XmlType()] 
public class DownloadPath
{
[XmlAttribute]
public string Src;
[XmlText]
public string Text;
}
// serialize to
< DownloadPath Src =...> text< / DownloadPath>


I have a set of custom XML configuration management classes in .NET.

There are two configuration scopes in my application:

User: the setting applies to the user regardless of the account she's logged into. Stored in a XML config file in the user's directory.

User/Account: the setting applies to the user when logged into a particular account. Stored in a XML config file in the user's directory under an account specific sub-directory.

The two XML documents have the same structure and can be modified by hand. When the application starts up, I read the XML config from the user's profile and the XML config from the User/Account directory. I then merge the two XML documents into a single XDocument and then deserialize the XML into objects modeled after the XDocument's sections. If there is a User/Account-level setting present, it should override the User-level setting. Example:

User file:

<FileSettings>
  <DownloadPath>C:\downloads</DownloadPath>
  <UploadPath>C:\uploads</UploadPath>
</FileSettings>

User/Account file for account XYZ:

<FileSettings>
  <DownloadPath>C:\newlocation\xyz\mystuff</DownloadPath>
</FileSettings>

Result after merge:

<FileSettings>
  <DownloadPath>C:\newlocation\xyz\mystuff</DownloadPath>
  <UploadPath>C:\uploads</UploadPath>
</FileSettings>

In the above example, a FileSettings object with 2 properties - DownloadPath and UploadPath will be instantiated from the section.

Here's my issue: I have no idea which config file (i.e., scope) the DownloadPath and UploadPath came from. As a result, when the object needs to serialize again, it doesn't know which properties go in which file.

Question: What's the best way to store the "source," on a property-by-property basis, so that I can ensure a setting gets written to the same config file that it was read from?

Thanks.

解决方案

After merging, is it possible to append the value source of each properties?

<FileSettings>
  <DownloadPath>C:\newlocation\xyz\mystuff</DownloadPath>
  <UploadPath>C:\uploads</UploadPath>
  <DownloadPathSrc>xyz</DownloadPathSrc>
  <DownloadPathSrc>user</DownloadPathSrc>
</FileSettings>

If so, the two src properties can be deserialized into your FileSettings object, Before serialization to file again, you can create a brand new FileSetting object for each of the source WITHOUT setting the *src property, by default null string won't be serialized by XmlSerializer. With the src property, you know which file the object you want to serialize to.

EDIT, If you want to add src as attribute of the element. that was actually my intial thought too, however, you need create class for each of the property. e.g. for property DownloadPath in FileSetting class

[XmlType()]
public class DownloadPath
{ 
  [XmlAttribute]
  public string Src;   
  [XmlText]
  public string Text;
}
// serialize to
<DownloadPath Src="...">text</DownloadPath>

这篇关于序列化/反序列化到/从多个XML配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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