bu alternatif kolay yolu deneyin: İşte xml sonucu
void Main()
{
var source=
new TestClass()
{
GroupTestTyped=
new Dictionary<string, int> { {"A", 23}, {"B", 40} }
};
using (var writer = XmlWriter.Create("c:\\test1.xml"))
(new XmlSerializer(typeof(TestClass))).Serialize(writer, source);
}
[Serializable]
public class DemoElementClass
{
public string Key { get; set; }
public int Value { get; set; }
}
[Serializable]
public class TestClass
{
public TestClass() { }
[XmlArray]
[XmlArrayItem(ElementName = "ElementTest")]
public List<DemoElementClass> GroupTest { get; set; }
[XmlIgnore]
public Dictionary<string, int> GroupTestTyped
{
get { return GroupTest.ToDictionary(x=> x.Key, x => x.Value); }
set { GroupTest = value.Select(x => new DemoElementClass() {Key = x.Key, Value = x.Value}).ToList(); }
}
}
:
[XmlElement("Dictionary")]
public List<KeyValuePair<string, string>> XMLDictionaryProxy
{
get
{
return new List<KeyValuePair<string, string>>(this.Dictionary);
}
set
{
this.Dictionary = new Dictionary<string, string>();
foreach (var pair in value)
this.Dictionary[pair.Key] = pair.Value;
}
}
[XmlIgnore]
public Dictionary<string, string> Dictionary
{
get; set;
}
: Burada
<?xml version="1.0" encoding="utf-8"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GroupTest>
<ElementTest>
<Key>A</Key>
<Value>23</Value>
</ElementTest>
<ElementTest>
<Key>B</Key>
<Value>40</Value>
</ElementTest>
</GroupTest>
</TestClass>
İyi bir raporda, sözlüklerin .NET 4.0'da seri hale getirilebildiğini biliyorum. – kbrimington
Özel eleman isimleri ve özellikleriyle nasıl yapılır? – Burnzy
[Sözlükleri Dizine Ekleme'nin Tek Yolu] konusuna bakın (http://johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!699.entry). –