简体   繁体   English

如何根据其属性值删除 XML 元素

[英]How do I remove an XML element based on the value of its attribute

I'm having difficulties wrapping my head around how to do this, and since it's my first time doing anything with xml, I figured I should ask more experienced people to save some time.我在思考如何做到这一点时遇到了困难,而且由于这是我第一次使用 xml 做任何事情,我想我应该请更有经验的人来节省一些时间。

I know how to remove elements.我知道如何删除元素。 I've loaded the xml into an XDocument.我已将 xml 加载到 XDocument 中。 I know how to read elements, and obtain the value of an attribute of an element.我知道如何读取元素,并获取元素的属性值。 I just don't know how to remove multiple elements of the same type based on the value of their attributes.我只是不知道如何根据属性值删除相同类型的多个元素。 This is a sample of the XML I want to use.这是我要使用的 XML 示例。 I want to remove all effect elements with type="SetName"我想删除type="SetName"所有效果元素

<tech name="Name" type="Normal">
    <displaynameid>11166</displaynameid>
    <effects>
        <effect type="TextOutput"></effect>
        <effect type="Data"></effect>
        <effect type="Data"></effect>
        <effect type="SetName"></effect>
        <effect type="SetName"></effect>
        <effect type="Data"></effect>
        <effect type="SetName"></effect>
        <effect type="SetName"></effect>
        <effect type="SetName"></effect>
    </effects>
</tech>

This is not an answer to your question because your question is too abstract.这不是您问题的答案,因为您的问题太抽象了。 But since it will be a long comment therefore I am writing it as an answer.但由于这将是一个很长的评论,因此我将其写为答案。

Let's go step by step about your problem.让我们逐步解决您的问题。

If you don't know how to read xml in c# then you can read about XElement or XmlReader如果您不知道如何在 c# 中读取 xml,那么您可以阅读XElementXmlReader

If you don't know how to read xml elements then google something like "traversing xml c#"如果你不知道如何阅读 xml 元素,那么谷歌类似于“遍历 xml c#”

if you don't know how to read element attributes then XElement has Attributes property which returns a collection of XAttribute which has property Value如果您不知道如何读取元素属性,则XElement具有Attributes属性,该Attributes返回具有属性ValueXAttribute集合

if you don't know how to delete child element then see the following link How to delete specific nodes from an XElement?如果您不知道如何删除子元素,请参阅以下链接如何从 XElement 中删除特定节点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM