您可以使用 Linq to Xml 从您的 SampleClass 对象创建所需的 xml:
SampleClass sample = new SampleClass();
sample.SampleProperties = new Dictionary<string, List<string>>() {
{ "Name", new List<string>() { "Greg", "Tom" } },
{ "City", new List<string>() { "London", "Warsaw" } }
};
var result = new XElement("DataItem",
sample.SampleProperties.Select(kvp =>
new XElement(kvp.Key,
kvp.Value.Select(value => new XElement("value", value)))));
result.Save(path_to_xml);
输出:
<DataItem>
<Name>
<value>Greg</value>
<value>Tom</value>
</Name>
<City>
<value>London</value>
<value>Warsaw</value>
</City>
</DataItem>
从 xml 反序列化:
SampleClass sample = new SampleClass();
sample.SampleProperties = XElement.Load(path_to_xml).Elements().ToDictionary(
e => e.Name.LocalName,
e => e.Elements().Select(v => (string)v).ToList());