0

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?

Kingpin
  • 1,067
  • 2
  • 14
  • 34
  • I have tried to use something like this: while (reader.Read()) { if (reader.Name == "KeyValuePairThatSerializesProperlyOfInt32Boolean") { } } – Kingpin Jan 11 '12 at 09:32
  • But i have a no idea how to get the vale and key together... – Kingpin Jan 11 '12 at 09:33
  • Have you tried using the XmlSerializer? That XML looks suspiciously like XML produced by it's standard serialization routines; and so should easily load the object back. – Andras Zoltan Jan 11 '12 at 10:10

1 Answers1

2

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

}
k.m
  • 30,794
  • 10
  • 62
  • 86
  • This throws an exception at var document = XDocument.Load(reader); The exception is: {"The XmlReader state should be EndOfFile after this operation."} – Kingpin Jan 11 '12 at 10:14