Shafique
Shafique

Reputation: 1826

Convert XML to C# Dictionary

I've got an XML response that looks like:

<?xml version="1.0" encoding="utf-8"?>
<response>
  <scheme>
    <name>name_1</name>
    <items>
      <item>
        <itemid>testid_1</itemid>
        <new></new>
        <used></used>
      </item>
      <item>
        <itemid>testid_2</itemid>
        <new></new>
        <used></used>
      </item>
    </items>
  </scheme>
  <scheme>
    <name>name_2</name>
    <items>
      <item>
        <itemid>testid_3</itemid>
        <new></new>
        <used></used>
      </item>
    </items>
  </scheme>
</response>

I've got to convert this XML response into a Dictionary<string, List<Item>>. Each entry in the dictionary is a <scheme> element, where the Dictionary's key is the <name> node, and the Dictionary's value is the List<Item>, where the Item object is comprised of itemid, new, and used.

So far I've got code that looks like:

var items =
            from item in itemsXml.Descendants("scheme")
            select new Item
            {
                ItemId = item.Element("itemid").Value,
                New = item.Element("new").Value,
                Used = item.Element("used").Value,
            };

So the code I've got above will create me a list of Item, but how do I get access to the <name> node in each <scheme> node? Again, the <name> is the key, and the List<Item> under each scheme is the value for my Dictionary.

Any suggestions on a different way to do this??!? I'm definitely not doing this right..

Upvotes: 0

Views: 3267

Answers (2)

taylonr
taylonr

Reputation: 10790

Does it HAVE to be a dictionary?

The reason I'm asking is, you could create an item class that has your item properties. Then create a scheme class that has a property called "Name" as well as a List property.

You could then use .NET deserialization to create a list of schemes, each scheme having a list of items.

Something like:

public class Response
{
    public IList<Scheme> Schemes {get; set;}
}

public class Scheme
{
    public string Name {get; set;}
    public IList<Item> Items {get; set;}
}

public class Item
{
    public string ItemId {get; set;}
    public bool New {get; set;}
    public bool Used {get; set;}
}

Then follow a tutorial like this one: http://sharpertutorials.com/serialization/ And let .Net do the deserialization for you.

Granted, this won't work if you have to do a dictionary.

Upvotes: 2

Ahmad Mageed
Ahmad Mageed

Reputation: 96487

Try this approach:

var dict = xml.Elements("scheme")
              .ToDictionary(e => e.Element("name").Value,
                            e => e.Descendants("item")
                                  .Select(item => new Item()
                                  {
                                      ItemId = item.Element("itemid").Value,
                                      New = item.Element("new").Value,
                                      Used = item.Element("used").Value
                                  }).ToList()
                           );

Upvotes: 4

Related Questions