Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The code below currently works and loads XML into a class structure. This structure consists of a collection of items (items), each that have fixed properties and a collection that I use for a variable number of properties.

Two things I want to do: 1) I want to change the list into something that has a unique key. I tried a dictionary and that didn't work, maybe a HashSet...leading on to... 2) The key for the collection should be driven by the item's "id" XML attribute.

I can't quite figure it out, and trying to copy the KVP pattern I used for an items variable parameters does not work. It adds the items to "items" but they are all empty and the hash for the collection is not populated.

Help please

PS. Seems tagging sections of text as C# and XML are not working.

[XmlRoot("ItemsContainer")]
public class Items
{
[XmlAttribute("Version")]
public string Version { get; set; }

[XmlArray("Items")]
[XmlArrayItem("Item")]
public List<item> items = new List<item>(); //TODO - The key in this collection should be unique and driven from an item's "Id" XML attribute

public static Items Load(string path)
{
var xml = Resources.Load<textasset>(path);
var serializer = new XmlSerializer(typeof (Items));
var reader = new StringReader(xml.text);
var items = (Items) serializer.Deserialize(reader);
reader.Close();
return items;
}
}


public class Item
{
//### Fixed Item Parameters
[XmlAttribute("Id")]
public int Id { get; set; }

[XmlElement("Name")]
public string Name { get; set; }

[XmlElement("Description")]
public string Description { get; set; }

[XmlElement("Value")]
public float Value { get; set; }

//### Variable Item Parameters as Key Value pairs.
[XmlArray("KVPS")]
[XmlArrayItem("KVP")]
public List<KeyValuePair<string, string>> KVPS { get; set; } //We will have cases were we don't want unique keys
}

[Serializable]
public class KVP<K, V>
{
public K Key { get; set; }
public V Value { get; set; }

public KVP()
{
}

public KVP(K key, V value)
{
Key = key;
Value = value;
}
}



Here is the associated XML

<?xml version="1.0" encoding="utf-8"?>

<ItemsContainer Version="1.0">
<items>

<Item Id="100">
<name>Burger
<description>The lovely tasting Big Mac
<value>5.67


<Item Id="101">
<name>Sammich
<description>Ham and cheese
<value>2.80


<Item Id="102">
<name>Carling
<description>Pint of carling
<value>2.80
<kvps>
<kvp>
<key>alchohol
<value>3.9

<kvp>
<key>alchohol
<value>4.9

<kvp>
<key>alchohol
<value>5.9

What I have tried:

I can't quite figure it out, and trying to copy the KVP pattern I used for an items variable parameters does not work. It adds the items to "items" but they are all empty and the hash for the collection is not populated.
Posted
Updated 8-May-17 7:16am
v2

1 solution

Assuming the elements in your XML file are actually cased correctly, and the missing closing elements are present, you just need to change the type of your KVPS property from:
List<KeyValuePair<string, string>>
to
List<KVP<string, string>>

You're currently using the built-in System.Collections.Generic.KeyValuePair<TKey, TValue> type, which doesn't have a default constructor defined. If you switch to using your custom type, the deserialization will work as expected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900