C# 使用LINQ搜索XML元素
我有以下XML文件:C# 使用LINQ搜索XML元素,c#,asp.net,xml,linq,xpath,C#,Asp.net,Xml,Linq,Xpath,我有以下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</des
<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>
我想返回标签中SN==Textbox.text的(名称、说明、价格、数量)
。您可以使用LINQ、XPATH或任何其他方法来帮助我们吗
我已经尝试过这段代码,但我不知道如何在其中插入SN==Textbox.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 />";
}
xdocumentxmldoc=XDocument.Load(Server.MapPath(“/XML/Cat1.XML”);
var persons=来自xmlDoc.subjects(“项目”)中的人员
选择新的
{
SN=个人要素(“SN”)值,
name=person.Element(“name”).Value,
数量=人。元素(“数量”)。值,
description=person.Element(“description”).值,
价格=个人要素(“价格”)价值,
};
foreach(var个人对个人)
{
label1.Text=person.SN“
”;
label2.Text=person.name“
”;
label3.Text=person.description“
”;
label4.Text=person.price“
”;
label5.Text=person.quantity“
”;
}
当您希望对数据源应用动态筛选器时,我建议您执行以下操作:
选择要筛选的内容
根据需要对其进行过滤
从筛选的内容中选择值
根据需要重复步骤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
你能编辑一下我的代码吗。var persons=来自xmlDoc.subjects(“cat”)中的person,其中id==dropdownlist.text&&sn==textbox xml.doc(item)很抱歉描述和语法不好,但我确实需要这样编辑它,如果你知道我的意思,并且你的代码给了我IEnumerable的问题:不能与类型一起使用argument@RefaatKh我不明白你为什么“需要”它应该是查询理解语法,但请参阅我的编辑。我想我也修复了你看到的错误。它很有效,thx很多。我重新修正了错误
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"),
};