【问题标题】:LINQ complex delete and search queryLINQ 复杂的删除和搜索查询
【发布时间】: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:我想删除 &lt;item&gt; 元素及其使用 linq 的子元素,其中 &lt;cat id="computer"&gt;&lt;SN&gt; 具有特定值,例如 44 。

问题 2:我想使用 TextBox 和 Literal1 进行查询,它们返回特定的 &lt;item&gt; 及其子的 .这个查询应该在 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(); }
  • 通过编辑您的问题更好地添加您的努力。
  • 您的查询看起来不错。您面临什么问题?
  • 请看主要问题
  • 我想通过添加条件来编辑此查询

标签: c# xml linq textbox


【解决方案1】:

试试这个:-

xmlDoc.Root.Descendants("cat").Where(x => x.Attribute("id").Value == "computer")
      .Descendants("item").Where(x => x.Element("SN").Value.Trim() == Dropdownlist.Text)
      .Remove();
xmlDoc.Save(@"YourXML.xml");

对于过滤,您可以替换您喜欢的特定位置的值。另外,我直接使用了Trim,最好先在您的实际代码中检查空字符串。

问题2:-

var result = xmlDoc.Descendants("item")
             .Where(x => x.Element("SN").Value.Trim() == Dropdownlist.Text);

【讨论】:

  • 你能不能编辑一下,让它在 SN == Dropdownlist.text 的位置??
  • @RefaatKh - 请检查 nw。
  • 非常感谢它有效。那么问题2呢?你有什么解决办法吗?
  • @RefaatKh - 不客气。不要按cat过滤,第二个问题省略那部分,直接使用item。我也会更新的。
  • 它的工作原理非常感谢我的朋友。我真的很感激。
【解决方案2】:

关于问题一,我会使用方法语法做这样的事情:

  doc.Descendants("cat")
        .Where(x => String.Equals((string)x.Attribute("id"), "computer", StringComparison.OrdinalIgnoreCase))
        .Elements("item")
        .Where(x => (string)x.Element("SN") == "44")
        .Remove();

基本上选择所有 cat 元素并按属性 id = computer.然后选择每个项目中的所有项目并通过 SN = 44 过滤。

【讨论】:

  • 你能不能编辑一下,让它在 SN == Dropdownlist.text 的位置??
猜你喜欢
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多