使用 LINQ 解析 XML 问题
我想从 XML 文件中获取一些值并使用 LINQ 将它们插入到 ListBox 中。我哪里错了?
<?xml version="1.0" encoding="UTF-8"?>
<tells>
<defindividual name="name1"/>
<instanceof>
<individual name="name1"/>
<catom name="value"/>
</instanceof>
<defindividual name="name2"/>
<instanceof>
<individual name="name2"/>
<catom name="value"/>
</instanceof>
<defindividual name="name3"/>
<instanceof>
<individual name="name3"/>
<catom name="otherValue"/>
</instanceof>
</tells>
隐藏代码:
protected void Button1_Click(object sender, EventArgs e)
{
XDocument owlXML = XDocument.Load(Server.MapPath("App_Data\\myFile.xml"));
var items = from item in owlXML.Descendants("instanceof")
where item.Element("catom").Attribute("name").Value == "value"
select new
{
catom = item.Element("catom").Attribute("name").Value
};
foreach (var item in items)
{
//ListBox1.DataSource = item;
//ListBox1.DataBind();
ListBox1.Items.Add(item.catom);
}
}
I want to get some values from the XML file and insert them into a ListBox using LINQ. Where am I wrong?
<?xml version="1.0" encoding="UTF-8"?>
<tells>
<defindividual name="name1"/>
<instanceof>
<individual name="name1"/>
<catom name="value"/>
</instanceof>
<defindividual name="name2"/>
<instanceof>
<individual name="name2"/>
<catom name="value"/>
</instanceof>
<defindividual name="name3"/>
<instanceof>
<individual name="name3"/>
<catom name="otherValue"/>
</instanceof>
</tells>
Code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
XDocument owlXML = XDocument.Load(Server.MapPath("App_Data\\myFile.xml"));
var items = from item in owlXML.Descendants("instanceof")
where item.Element("catom").Attribute("name").Value == "value"
select new
{
catom = item.Element("catom").Attribute("name").Value
};
foreach (var item in items)
{
//ListBox1.DataSource = item;
//ListBox1.DataBind();
ListBox1.Items.Add(item.catom);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者
or