2012-01-11 60 views
0
名單

我的類實現IXmlSerializable的,具有這樣的特性:如何deserialzie KeyValuePair

public List<KeyValuePair<int, bool>> exportList 
    { 
     get { return _exportList; } 
     set { _exportList = value; } 
    } 

我有一個XML文件,並必須填寫

public void ReadXml(XmlReader reader) 
{ 
} 

我的XML與entrys名單 - 文檔看起來像這樣:

<Object msdata:InstanceType="CYNOX_Datenlogger_Software.Geräte.Slave, CYNOX_Datenlogger_Software, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Name="Device 4" ID="4" IDParent="3" PrimeAddress="0" SecondaryAdd="10520089" AdditionalInfo="" Locked="False" StandAlone="True" ManuID="ELS" csvPath="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>false</Value> 
    <Key>0</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>true</Value> 
    <Key>1</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>true</Value> 
    <Key>2</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>false</Value> 
    <Key>3</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>false</Value> 
    <Key>4</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>false</Value> 
    <Key>5</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>false</Value> 
    <Key>6</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>false</Value> 
    <Key>7</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
    <KeyValuePairThatSerializesProperlyOfInt32Boolean xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Value>false</Value> 
    <Key>8</Key> 
    </KeyValuePairThatSerializesProperlyOfInt32Boolean> 
</Object> 

我該如何做到這一點?

+0

我曾嘗試使用這樣的: 而(reader.Read()){ 如果 (reader.Name ==「 KeyValuePairThatSerializesProperlyOfInt32Boolean「) { }} – Kingpin 2012-01-11 09:32:31

+0

但我有一個不知道如何讓谷和密鑰一起... – Kingpin 2012-01-11 09:33:01

+0

您是否嘗試過使用XmlSerializer?這個XML看起來像它的標準序列化例程產生的XML一樣可疑;所以應該輕鬆地將物體裝回。 – 2012-01-11 10:10:46

回答

2

可以利用到XML LINQ的一點:

public void ReadXml(XmlReader reader) 
{ 
    var document = XDocument.Load(reader); 
    this._exportList = document 
     .Descendants("KeyValuePairThatSerializesProperlyOfInt32Boolean") 
     .Select(e => new KeyValuePair<int, bool>(
      Int32.Parse(e.Element("Key").Value), 
      Boolean.Parse(e.Element("Value").Value) 
     )).ToList(); 

} 
+0

這會在var document = XDocument.Load(reader)中引發異常; 例外情況是: {「在執行此操作後,XmlReader狀態應爲EndOfFile」。} – Kingpin 2012-01-11 10:14:08

+0

我添加了ReadSubtree,現在它工作的很完美。非常感謝... – Kingpin 2012-01-11 10:22:34