当我们有多个具有相同名称但属性不同的元素时,如何使用Xdocument从xml中删除元素

时间:2021-12-08 09:11:45

I have an xml document which looks like this:

我有一个xml文档,如下所示:

<Applications>
  <myApp>
    <add key="ErrorDestinationEventLog" value="EventLog" />
    <add key="version" value="5.0.0.0" />
    <add key="DebugMode_RUN" value="true" />
  </myApp>
</Applications>

All the elements have same element name but different attributes. How do I remove one particular element and it's attributes from this xml using XDocument in C#?

所有元素都具有相同的元素名称但属性不同。如何在C#中使用XDocument从此xml中删除一个特定元素及其属性?

xd.Element("Applications").Element("myApp").Element(xe.Name).RemoveAll();

The above command is not working as all the elements have same name.

上述命令不起作用,因为所有元素都具有相同的名称。

Is there any way to identify an element with, other than it's name? And if so, how can I use this to remove it from the XDocument?

除了它的名字之外,还有什么办法可以识别一个元素吗?如果是这样,我如何使用它从XDocument中删除它?

2 个解决方案

#1


18  

string key = "version";
XDocument xdoc = XDocument.Load(path_to_xml);
xdoc.Descendants("add")
    .Where(x => (string)x.Attribute("key") == key)
    .Remove();

UPDATE You almost did the job. What you missed is filtering elements by attribute value. Here is your code with filtering and removing selected elements:

更新你几乎完成了这项工作。您错过的是按属性值过滤元素。以下是过滤和删除所选元素的代码:

xd.Element("Applications")
  .Element("myApp")
  .Elements("add")
  .Where(x => (string)x.Attribute("key") == key)
  .Remove();

#2


3  

xd.Descendants("add")
    .First(a => a.Attribute("key").Value == "version")
    .Remove();

If you have tags other than myApp under Applications containing add, you may prefer a safer version

如果在包含add的Applications下有myApp以外的标签,您可能更喜欢更安全的版本

xd.Descendants("myApp").First()
    .Descendants("add")
    .Where(x => (string)x.Attribute("key") == "version")
    .Remove();

You can also use XPath (System.Xml.XPath)

你也可以使用XPath(System.Xml.XPath)

string key="version";
xd.XPathSelectElement(String.Format("//myApp/add[@key='{0}']",key)).Remove();

#1


18  

string key = "version";
XDocument xdoc = XDocument.Load(path_to_xml);
xdoc.Descendants("add")
    .Where(x => (string)x.Attribute("key") == key)
    .Remove();

UPDATE You almost did the job. What you missed is filtering elements by attribute value. Here is your code with filtering and removing selected elements:

更新你几乎完成了这项工作。您错过的是按属性值过滤元素。以下是过滤和删除所选元素的代码:

xd.Element("Applications")
  .Element("myApp")
  .Elements("add")
  .Where(x => (string)x.Attribute("key") == key)
  .Remove();

#2


3  

xd.Descendants("add")
    .First(a => a.Attribute("key").Value == "version")
    .Remove();

If you have tags other than myApp under Applications containing add, you may prefer a safer version

如果在包含add的Applications下有myApp以外的标签,您可能更喜欢更安全的版本

xd.Descendants("myApp").First()
    .Descendants("add")
    .Where(x => (string)x.Attribute("key") == "version")
    .Remove();

You can also use XPath (System.Xml.XPath)

你也可以使用XPath(System.Xml.XPath)

string key="version";
xd.XPathSelectElement(String.Format("//myApp/add[@key='{0}']",key)).Remove();