【发布时间】: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