Question

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

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top