【发布时间】:2014-12-25 18:34:44
【问题描述】:
//假设我有以下 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>
<item>
</cat>
</warehouse>
问题 1:我想删除 <item> 元素及其使用 linq 的子元素,其中 <cat id="computer"> 和 <SN> 具有特定值,例如 44 。
问题 2:我想使用 TextBox 和 Literal1 进行查询,它们返回特定的 <item> 及其子的 .这个查询应该在 linq 中。
例如
XDocument xmlDoc = XDocument.Load(Server.MapPath("/XML/Cat1.xml"));
var persons = from person in xmlDoc.Descendants("item")
where person.Element("SN").Value.Equals(DropDownList1.Text)
select person;
persons.Remove();
foreach (XElement person in persons.ToList())
{
person.Remove();
}
【问题讨论】:
-
我想要这样的代码 XDocument xmlDoc = XDocument.Load(Server.MapPath("/XML/Cat1.xml")); var people = from person in xmlDoc.Descendants("item") where person.Element("SN").Value.Equals(DropDownList1.Text) select person;人.删除(); foreach(persons.ToList() 中的 XElement person){ person.Remove(); }
-
通过编辑您的问题更好地添加您的努力。
-
您的查询看起来不错。您面临什么问题?
-
请看主要问题
-
我想通过添加条件来编辑此查询