【问题标题】:How to deserialzie List of KeyValuePair如何反序列化 KeyValuePair 列表
【发布时间】:2012-01-11 17:53:07
【问题描述】:

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

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

我有一个 XML 文档,并且必须在列表中填写条目

public void ReadXml(XmlReader reader)
{
}

我的 XML 文档如下所示:

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

我怎样才能做到这一点?

【问题讨论】:

  • 我尝试过使用这样的东西:while (reader.Read()) { if (reader.Name == "KeyValuePairThatSerializesProperlyOfInt32Boolean") { } }
  • 但我不知道如何将 vale 和 key 放在一起......
  • 您是否尝试过使用 XmlSerializer?该 XML 看起来很像由其标准序列化例程生成的 XML;因此应该很容易将对象加载回来。

标签: c# xml deserialization key-value readxml


【解决方案1】:

您可以使用一点 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();

}

【讨论】:

  • 这会在 var document = XDocument.Load(reader); 处引发异常;例外是:{“此操作后 XmlReader 状态应为 EndOfFile。”}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
相关资源
最近更新 更多