如何反序列化KeyValuePair列表

发布于 2024-12-26 06:11:53 字数 3319 浏览 1 评论 0原文

我的类实现 IXmlSerialized 并具有如下属性:

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

我有一个 XML 文档,并且必须使用我的 XML 文档中的条目填充列表,

public void ReadXml(XmlReader reader)
{
}

如下所示:

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

我怎样才能完成此操作?

My Class implements IXmlSerializable and has a property like this:

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

I have an XML Document and have to fill the list with entrys in

public void ReadXml(XmlReader reader)
{
}

My XML-Document looks like this:

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

How can i accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2025-01-02 06:11:53

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

}

You can utilize little bit of 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();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文