使用 Linq to XML 删除父元素

发布于 2025-01-01 15:22:25 字数 916 浏览 1 评论 0原文

我有一个这样的 XML 文件,

<BallList>
<Brand name="xyz">
    <BallName>ball A</BallName>
    <DateApproved>Jan-12</DateApproved>
    <link>www.a.com</link>
</Brand>
<Brand name="abc">
    <BallName>Ball B</BallName>
    <DateApproved>Jan-02</DateApproved>
    <link>www.b.com</link>
</Brand>
</BallList>

这样就有大约 150 个品牌和 8000 个球名。我知道这不是表示 XML 的好方法,但现在由于数据庞大,结构无法修改。

我需要通过将“Brand”节点与其球名进行比较来删除它,

这是我正在尝试的代码:

XElement doc = XElement.Load(Server.MapPath("NetBalls.xml"));
doc.Elements("Brand")
   .Where(s => s.Attribute("name").Value == DropDownList1.SelectedItem.Text)
   .Elements("BallName")
   .Where(l => l.Value == textbox1.Text)
   .AncestorsAndSelf()
   .Remove();

我正在尝试搜索品牌并进入其元素节点并检查其节点并删除其父节点。

谁能帮我解决这个问题吗?

I have an XML File of this sort

<BallList>
<Brand name="xyz">
    <BallName>ball A</BallName>
    <DateApproved>Jan-12</DateApproved>
    <link>www.a.com</link>
</Brand>
<Brand name="abc">
    <BallName>Ball B</BallName>
    <DateApproved>Jan-02</DateApproved>
    <link>www.b.com</link>
</Brand>
</BallList>

In This way there are around 150 brands and 8000 ball names. I know this is not a good way to represent the XML, but now as the data is huge, structure cannot be modified.

I need to delete "Brand" node by comparing it with its ballname

Here is the code that I am trying out:

XElement doc = XElement.Load(Server.MapPath("NetBalls.xml"));
doc.Elements("Brand")
   .Where(s => s.Attribute("name").Value == DropDownList1.SelectedItem.Text)
   .Elements("BallName")
   .Where(l => l.Value == textbox1.Text)
   .AncestorsAndSelf()
   .Remove();

I am trying to search for brand and go into its element node and check its node and delete its parent.

Can anyone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野侃 2025-01-08 15:22:25

无需向下导航到子节点并再次返回,只需将子级条件嵌入到针对您实际想要的元素的 Where 查询中:

doc.Elements("Brand")
   .Where(s => s.Attribute("name").Value == selectedBrand)
   .Where(s => s.Elements("BallName").Any(l => l.Value == selectedValue))
   .Remove();

这样,您的 Linq 查询与您的英语查询更加匹配:“找到所有名称属性为 x、Ballname 元素为 y 的 Brand 元素,并将其删除。”

Instead of navigating down to the child nodes and back up again, just embed the child-level criteria in a Where query against the elements you actually want:

doc.Elements("Brand")
   .Where(s => s.Attribute("name").Value == selectedBrand)
   .Where(s => s.Elements("BallName").Any(l => l.Value == selectedValue))
   .Remove();

This way, your Linq query more closely matches your English query: "Find me all of the Brand elements where the name attribute is x, and their Ballname element is y, and remove it."

您想删除品牌元素吗?试试这个:

xml.Elements("Brand")
   .Where(s => s.Attribute("name").Value == DropDownList1.SelectedItem.Text)
   .Elements("BallName")
   .Where(l => l.Value == textbox1.Text)
   .Select(x => x.Parent)
   .Remove();

这将删除所有适用条件的品牌元素。

You want to remove the brand element? Try this:

xml.Elements("Brand")
   .Where(s => s.Attribute("name").Value == DropDownList1.SelectedItem.Text)
   .Elements("BallName")
   .Where(l => l.Value == textbox1.Text)
   .Select(x => x.Parent)
   .Remove();

This will remove all Brand elements where the conditions apply.

瑾兮 2025-01-08 15:22:25

您可以将选择条件组合到单个 where 子句中:

doc.Elements("Brand")
    .Where(brand =>
        (string)brand.Attribute("name") == DropDownList1.SelectedItem.Text &&
        brand.Elements("BallName").Any(ballName => (string)ballName == textbox1.Text))
    .Remove();

You can combine the selection criteria in a single where clause:

doc.Elements("Brand")
    .Where(brand =>
        (string)brand.Attribute("name") == DropDownList1.SelectedItem.Text &&
        brand.Elements("BallName").Any(ballName => (string)ballName == textbox1.Text))
    .Remove();
你是我的挚爱i 2025-01-08 15:22:25
 string brandName = "xyz";
 string ballName = "ball A";
 var brands = doc.Elements("Brand").Where(s => s.Attribute("name").Value == brandName);
 foreach (var brand in brands)
 {
     if(brand.Elements("BallName").Any(l => l.Value == ballName))
     {
         brand.Remove();
     }
 }
 string brandName = "xyz";
 string ballName = "ball A";
 var brands = doc.Elements("Brand").Where(s => s.Attribute("name").Value == brandName);
 foreach (var brand in brands)
 {
     if(brand.Elements("BallName").Any(l => l.Value == ballName))
     {
         brand.Remove();
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文