如何反序列化KeyValuePair列表

时间:2012-01-11 09:16:17

标签: c# xml deserialization key-value readxml

My Class实现IXmlSerializable并具有如下属性:

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

我有一个XML文档,必须在

中填写列表
public void ReadXml(XmlReader reader)
{
}

我的XML-Document看起来像这样:

<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>

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用LINQ to XML:

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();

}