+ Reply to Thread
Results 1 to 2 of 2

Thread: XML and IDictionary

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    35

    XML and IDictionary

    My XML file is as follows,
    Code:
    <state name ="Alaska">
     <Location Name="l1">
      <Address>address</Address>
      <DateNTime>Saturday, sep 1, 2pm</DateNTime>
     </Location>
     <Location Name="l2">
      <Address>address2</Address>
      <DateNTime>Saturday, jan 11, 10pm</DateNTime>
     </Location>
    </state>
    I have 50 states. Every state will be in dropdown list and on click of the state the different locations with their address and times need to be displayed in grid view. This is the code
    Code:
    private static IDictionary<string, Dictionary<string, Property>> dictionary;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            XDocument doc = XDocument.Load(Server.MapPath("test2.xml"));
    
           dictionary = doc.Root.Elements("state").ToDictionary(
                state => state.Attribute("name").Value,
                state => state.Elements("Location").ToDictionary(
                    location => location.Attribute("Name").Value,
                    Property));
    
            var x = dictionary.Keys;
            DropDownList1.DataSource = x;
            DropDownList1.DataBind();
     }
    }
    public void OnSelectedIndexChanged(Object sender, EventArgs e)
    {
    
        GridView1.DataSource = from item in dictionary[DropDownList1.SelectedItem.Text]
                               select new { col1 = item.Key, col2 = item.Value };
        GridView1.DataBind();
    
    }
    
    public class Property
    {
      public string address;
      public string datetime;
    }
    I do not know how to declare IDictionary and retrieve data.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    40
    Please Try this:
    Code:
    dic = doc.Root.Elements("state").ToDictionary(
                    state => state.Attribute("name").Value,
                    state => state.Elements("Location").ToDictionary(
                        location => location.Attribute("Name").Value,
                        location => new Property
                        {
                            address = location.Element("Address").Value,
                            datetime = location.Element("DateNTime").Value
                        }));

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts