Wie kann ich Schlüsselpaar Liste (int Key, string Wert?) Bauen und aktualisieren Sie es, nachdem ich neue xml erhalten:Gebäude und aktualisieren statische Liste
List<KvPair> source = new List<KvPair>
{
new KvPair {Key = null, Value = "Null"},
new KvPair {Key = 0, Value = "No"},
new KvPair {Key = 1, Value = "Yes"}
};
Liste hängt von xml. Wenn ich die Liste neu geladen und nur 2 Werte von xml erhalten: null, 0, dann sollte die Liste nur zwei Elemente:
new KvPair {Key = null, Value = "Null"},
new KvPair {Key = 1, Value = "Yes"}
Soll ich statische Liste verwenden? Ist besserer Weg als das:
private List<KvPair> RebuildList(object extendData) //extendedData is xml
{
List<KvPair> source = new List<KvPair>
{
new KvPair {Key = null, Value = "Null"},
new KvPair {Key = 0, Value = "No"},
new KvPair {Key = 1, Value = "Yes"}
};
List<int> target = new List<int>();
var data = XDocument.Parse(extendData.ToString()).Descendants("elem");
int tempVal;
foreach (var elem in data)
{
var key = Int32.TryParse(elem.Attribute("Value").Value,
out tempVal) ? tempVal : null;
target.Add(key)
}
source.RemoveAll(x => !target.Contains(x));
return new List<KvPair>(source);
}