### Please note the very last comment: The OP just forgot to call `Save()`.

---

I am checking XML for attribute **ProductCount** under **ProductDetails** node, and if the attribute is not present add the attribute with a default value under this node.

I am able to check if the attribute exists or not but I am not able to add it, though it does not give me any error but does not even add the attribiute.

here is my code:

```
 XDocument XMLDoc = XDocument.Load(fileName);

 foreach (var detail in XMLDoc.Descendants(_ns + "ProductDetails"))
 {
    if (detail.Attribute("ProductCount") == null)
    {
        detail.SetAttributeValue("ProductCount", "1");
    }
 }

```

\_ns has my namespace.

I am not able to figure out what I am doing wrong, why is it not adding ProductCount attribute if it does not exist.