0

私のXMLファイルは次のとおりです。

<state name ="Alaska">
 <Location Name="loc1">
  <Address>xyz</Address>
  <DateNTime>Saturday, Oct 2, 8pm</DateNTime>
 </Location>
 <Location Name="loc2">
  <Address>abc</Address>
  <DateNTime>Saturday, Oct 2, 10am</DateNTime>
 </Location>
</state>

このように私は50の州を持っています。すべての状態がドロップダウンリストに表示され、状態をクリックすると、住所と時刻が記載されたさまざまな場所がグリッドビューに表示される必要があります。これはコードです

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;
}

ここでは、IDictionaryを宣言し、それに応じてデータを取得する方法を正確に知りません。誰かが私にそれを説明できますか?

4

1 に答える 1

1

これを試して:

dictionary = doc.Root.Elements("state").ToDictionary(
                s => s.Attribute("name").Value,
                s => s.Elements("Location").ToDictionary(
                    loc => loc.Attribute("Name").Value,
                    loc => new Property
                    {
                        address = loc.Element("Address").Value,
                        datetime = loc.Element("DateNTime").Value
                    }));
于 2012-01-26T21:27:09.520 に答える