使用 LINQ 搜索 XML 元素
本文关键字:元素 XML 搜索 LINQ 使用 | 更新日期: 2023-09-27 18:35:14
我有以下XML文件:
<warehouse>
<cat id="computer">
<item>
<SN>1</SN>
<name>Toshiba</name>
<quantity>12</quantity>
<description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
<price>400 USD</price>
</item>
<item>
<SN>22</SN>
<name>Toshiba</name>
<quantity>12</quantity>
<description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
<price>400 USD</price>
</item>
</cat>
<cat id="Stationery">
<item>
<SN> 33 </SN>
<name>note books</name>
<quantity>250</quantity>
<description>Caterpiller</description>
<price>5 USD</price>
</item>
</cat>
<cat id="Furniture">
<item>
<SN> 1 </SN>
<name>dasd</name>
<quantity>asdasd</quantity>
<description>das</description>
<price>dasd</price>
</item>
<item>
<SN>44</SN>
<name>Toshiba</name>
<quantity>12</quantity>
<description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
<price>400 USD</price>
</item>
</cat>
</warehouse>
我有以下用于生成查询的网络表单页面:(我需要声誉才能插入图像,所以我会尝试描述它)
choose where to search : (dropdownlist)
insert sn : (textbox)
generate query: (button)
[labels]
我想返回标签中SN==Textbox.text where <cat id= dropdownlist.text>
的(name,description,price,quantity)
。 您是否可以帮助使用 LINQ 或 XPATH 或任何其他方法。
我已经尝试过这段代码,但我不知道将SN==Textbox.text where <cat id= dropdownlist.text>
条件插入其中!! 请帮助我。
XDocument xmlDoc = XDocument.Load(Server.MapPath("/XML/Cat1.xml"));
var persons = from person in xmlDoc.Descendants("item")
select new
{
SN = person.Element("SN").Value,
name = person.Element("name").Value,
quantity = person.Element("quantity").Value,
description = person.Element("description").Value,
price = person.Element("price").Value,
};
foreach (var person in persons)
{
label1.Text = person.SN "<br />";
label2.Text = person.name"<br />";
label3.Text = person.description"<br />";
label4.Text = person.price"<br />";
label5.Text = person.quantity"<br />";
}
当您要对数据源应用动态筛选器时,我建议做的是:
- 选择要过滤的内容。
- 根据需要对其进行筛选。
- 从过滤的内容中选择值。
- 根据需要从步骤 2 重复。
所以你应该有这样的东西:
if (string.IsNullOrEmpty(dropdownlist.text) &&
string.IsNullOrEmpty(textbox.text))
{
// ERROR - must specify filter criteria
}
// select all cats
IEnumerable<XElement> cats = xmlDoc.Descendants("cat");
if (!string.IsNullOrEmpty(dropdownlist.text))
{
// filter by category
cats = cats.Where(c => (string)c.Attribute("id") == dropdownlist.text);
}
// select all items in the selected cats
IEnumerable<XElement> items = cats.SelectMany(c => c.Descendants("item"));
if (!string.IsNullOrEmpty(textbox.text))
{
// filter items by SN
items = items.Where(i => ((string)i.Element("SN")).Trim() == textbox.text);
}
var persons = from person in items
select new
{
SN = (string)person.Element("SN"),
name = (string)person.Element("name"),
quantity = (string)person.Element("quantity"),
description = (string)person.Element("description"),
price = (string)person.Element("price"),
};
// use persons as needed
以下是使用查询理解语法一次完成此操作的方法:
if (string.IsNullOrEmpty(dropdownlist.text) &&
string.IsNullOrEmpty(textbox.text))
{
// ERROR - must specify filter criteria
}
var persons = from cat in xmlDoc.Descendants("cat")
where (string.IsNullOrEmpty(dropdownlist.text) ||
(string)cat.Attribute("id") == dropdownlist.text)
from person in cat.Descendants("item")
where (string.IsNullOrEmpty(textbox.text) ||
((string)person.Element("SN")) == textbox.text)
select new
{
SN = (string)person.Element("SN"),
name = (string)person.Element("name"),
quantity = (string)person.Element("quantity"),
description = (string)person.Element("description"),
price = (string)person.Element("price"),
};