-2

Please help

I want too loop through all the below xml

and delete the "Att1" and Att2"

leaving all the others untouched

here is the example xml

<?xml version="1.0" encoding="utf-8"?>
<main>
    <example Att1="" Att2="" Att3="" Att4="" Att5="">
        <name>value</name>
        <name1>value</name1>
        <name2>value</name2>
        <name3>value</name3>
    </example>

    <example1 Att1="" Att2="" Att3="" Att4="" Att5="">
        <name>value</name>
        <name1>value</name1>
        <name2>value</name2>
        <name3>value</name3>
    </example1>

    <example2 Att1="" Att2="" Att3="" Att4="" Att5="">
        <name>value</name>
        <name1>value</name1>
        <name2>value</name2>
        <name3>value</name3>
    </example2>
</main>

sorry indeed I was wrong , unfortunately I can't post the original one

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Light_User
  • 83
  • 2
  • 11
  • Post a real xml. A tag can not have two attributes with the same name (``). then post what you have tried so far so that we can fix it. – L.B Feb 07 '14 at 22:55
  • You should look up Linq-To-Xml, or fiddle around with it by including `System.Linq.Xml` and instantiating an XDocument. It's fairly easy. – Magus Feb 07 '14 at 22:55
  • hi , yes L.B I was wrong about the XML ( changed it ) – Light_User Feb 08 '14 at 14:13

1 Answers1

1
var xDoc = XDocument.Load(filename); //XDocument.Parse(xmlstring);

xDoc.Descendants()
    .SelectMany(d => d.Attributes())
    .Where(a => a.Name == "Att1" || a.Name == "Att2")
    .ToList()
    .ForEach(x => x.Remove());

var newXml = xDoc.ToString(); //xDoc.Save(filename);
EZI
  • 15,209
  • 2
  • 27
  • 33
  • it works GREAT !!! Thanks !!! do you have an idea how can I add too loop the attributes I want eventually dynamically add values that I want to remove. I was thinking about adding text boxes or with a loop show the attributes and some how to select the one that I need , THANKS IN ADVANCE !! – Light_User Feb 08 '14 at 14:19