C# 中的 XML 和 IDictionary
我的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 并相应地检索数据。谁能给我解释一下吗?
My XML file is as follows,
<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>
In this way 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
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;
}
Here I am not knowing exactly how to declare IDictionary and retrieve data accordingly. Can anyone explain me that??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: