I have some XML in the following format:
<ObjectData>
<ModelName>MODEL_123</ModelName>
<ObjectName>OBJECT_A</ObjectName>
<Values>
<KeyValuePair>
<Key>NAME</Key>
<Value>PAUL</Value>
</KeyValuePair>
...
</Values>
</ObjectData>
...
I want to deserialize this into the following class:
[XmlRoot(Namespace = "")]
public class ObjectData
{
[XmlElement(Namespace = "")]
public string ModelName { get; set; }
[XmlElement(Namespace = "")]
public string ObjectName { get; set; }
[XmlArray]
public List<KeyValuePair<string, string>> Values { get; set; }
}
When I use this code, the KeyValuePair
s are not deserialized and the Values property is empty.
List<ObjectData> data = new List<ObjectData>();
XmlSerializer serializer = new XmlSerializer(typeof(ObjectData));
using (XmlReader reader = XmlReader.Create(new StringReader(inputXML)))
{
reader.MoveToContent();
ObjectData temp = (ObjectData)serializer.Deserialize(reader);
data.Add(temp);
}
Is the KeyValuePair
class not serializable in the way I use it? Or is there a problem in my ObjectData
class?
Try specifying the element names in your attributes:
[XmlArray("Values")]
[XmlArrayItem("KeyValuePair")]
public List<KeyValuePair<string, string>> Values { get; set; }
Combining D Stanley's answer with this post, I was able to come up with the right structure:
[XmlRoot(Namespace = "")]
public class ObjectData
{
[XmlElement(Namespace = "")]
public string ModelName { get; set; }
[XmlElement(Namespace = "")]
public string ObjectName { get; set; }
[XmlArray("Values")]
[XmlArrayItem("KeyValuePair")]
public List<KeyValuePair<string, string>> Values { get; set; }
}
[Serializable]
public class KeyValuePair<K, V> {
public K Key { get; set; }
public V Value { get; set; }
public KeyValuePair() { }
public KeyValuePair(K key, V value)
{
this.Key = key;
this.Value = value;
}
}
There is no setter for Key
or Value
in the KeyValuePair
struct. You'll have to change the data type. You can decorate the property so you can name the new type whatever you want.
....
[XmlArray("Values")]
[XmlArrayItem("KeyValuePair")] //not needed if MyItem is named KeyValuePair
public List<MyItem> Values { get; set; }
}
public class MyItem
{
public string Key { get; set; }
public string Value { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With