-1

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.

I know how to remove elements. I've loaded the xml into an 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. I want to remove all effect elements with 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>
DeuSJS
  • 1
  • 1
  • 2
    Welcome DeuSJS! Could you please provide some examples of what the data looks like, and what you've tried thus far? We can better provide help then :) – parameter Aug 22 '18 at 15:23
  • okay, just edited it. – DeuSJS Aug 22 '18 at 15:49
  • see the answer on this stackoverflow question. It is exactly the same question but obviously different xml data. https://stackoverflow.com/questions/28169101/how-to-delete-specific-nodes-from-an-xelement – adeel41 Aug 22 '18 at 15:50
  • Possible duplicate of [How to delete specific nodes from an XElement?](https://stackoverflow.com/questions/28169101/how-to-delete-specific-nodes-from-an-xelement) – adeel41 Aug 22 '18 at 15:51
  • 1
    basically you can't delete when you are looping through xml elements. So keep adding elements of interest in a list and then after the loop delete them – adeel41 Aug 22 '18 at 15:52
  • every way I've tried to filter out the elements has ended in either them not getting filtered out, or an exception being thrown, so I need exact code, so that I can learn for next time. – DeuSJS Aug 22 '18 at 16:03
  • ah, I figured it out, I forgot to go down a layer – DeuSJS Aug 22 '18 at 16:10

1 Answers1

0

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

If you don't know how to read xml elements then google something like "traversing 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

if you don't know how to delete child element then see the following link How to delete specific nodes from an XElement?

adeel41
  • 3,123
  • 1
  • 29
  • 25