m0fo
m0fo

Reputation: 2189

Replace XML attributes

I have an XML file, that contain IDs in an attribute called translatedID.

This attribute exists in almost every element in the doc, so instead of searching for each element one by one, I want to select all the translatedID in the document and run some code on each attribute's value.

The needed result is - replacing the translatedID with some other data.

How can I select ALL the translatedID that exists in my XML document?

Upvotes: 1

Views: 215

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503379

Sounds like a great case for LINQ to XML:

XDocument doc = XDocument.Load("doc.xml");
var attributes = doc.Descendants().Attributes("translatedID");

foreach (var attribute in attributes)
{
    attribute.Value = DoSomethingWith(attribute.Value);
}

doc.Save("transformed.xml");

(That's assuming you want to change the value of the attribute. If you want to do something else, that's doable - but you'll need to give more information.)

Upvotes: 2

Related Questions