gpt4 book ai didi

c# - 序列化字典包装器的问题

转载 作者:行者123 更新时间:2023-11-30 22:40:36 25 4
gpt4 key购买 nike

我定义了两个类。第一个...

[Serializable]
public class LocalizationEntry
{
public LocalizationEntry()
{
this.CatalogName = string.Empty;
this.Identifier = string.Empty;
this.Translation = new Dictionary<string, string>();
this.TranslationsList = new List<Translation>();
}

public string CatalogName
{
get;
set;
}

public string Identifier
{
get;
set;
}

[XmlIgnore]
public Dictionary<string, string> Translation
{
get;
set;
}

[XmlArray(ElementName = "Translations")]
public List<Translation> TranslationsList
{
get
{
var list = new List<Translation>();

foreach (var item in this.Translation)
{
list.Add(new Translation(item.Key, item.Value));
}

return list;
}
set
{
foreach (var item in value)
{
this.Translation.Add(item.Language, item.Text);
}
}
}
}

...哪里public List<Translation> TranslationsList是不可序列化的包装器 public Dictionary<string, string> Translation .

键值对定义如下:

[Serializable]
public class Translation
{
[XmlAttribute(AttributeName = "lang")]
public string Language
{
get;
set;
}

[XmlText]
public string Text
{
get;
set;
}

public Translation()
{

}

public Translation(string language, string translation)
{
this.Language = language;
this.Text = translation;
}
}

最后用于序列化的代码:

static void Main(string[] args)
{
LocalizationEntry entry = new LocalizationEntry()
{
CatalogName = "Catalog",
Identifier = "Id",
};

entry.Translation.Add("PL", "jabłko");
entry.Translation.Add("EN", "apple");
entry.Translation.Add("DE", "apfel");

using (FileStream stream = File.Open(@"C:\entry.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(LocalizationEntry));
serializer.Serialize(stream, entry);
}

LocalizationEntry deserializedEntry;
using (FileStream stream = File.Open(@"C:\entry.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(LocalizationEntry));
deserializedEntry = (LocalizationEntry)serializer.Deserialize(stream);
}
}

问题是反序列化后deserializedEntry.TranslationsList是空的。我在 LocalizationEntry.TransalionsList 的 setter 处设置了一个断点它也来自反序列化器空的。连载的产品当然有效。我的代码有什么漏洞吗?

编辑:

这是生成的 XML:

<?xml version="1.0"?>
<LocalizationEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CatalogName>Catalog</CatalogName>
<Identifier>Id</Identifier>
<Translations>
<Translation lang="PL">jabłko</Translation>
<Translation lang="EN">apple</Translation>
<Translation lang="DE">apfel</Translation>
</Translations>
</LocalizationEntry>

最佳答案

问题是 Xml Deserializer 没有设置您的 TranslationList 属性。 set 方法将被命中,但只能通过调用 this.TranslationsList = new List();在 LocalisationEntry 构造函数中。我还不确定为什么,但我怀疑是因为它不知道如何将 Translation 对象数组转换回 List。

我添加了以下代码,它运行良好:

[XmlArray(ElementName = "Translations")]
public Translation[] TranslationArray
{
get
{
return TranslationsList.ToArray();
}

set
{
TranslationsList = new List<Translation>(value);
}
}

[XmlIgnore]
public List<Translation> TranslationsList
....

关于c# - 序列化字典包装器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5117308/

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