I prefer to use XDocument , because simply you can search for it and change elements or attributes:
XDocument doc1 = XDocument.Parse("<AnswerSet> <Answer questionId=\"10\" FName=\"test\"> </Answer></AnswerSet> "); // or if you have related file simply use XDocument doc1 = XDocument.Load(fileFullName); var element = doc1.Descendants("AnswerSet").Elements("Answer") .Where(x => x.Attribute("FName") != null && x.Attribute("FName").Value == "test").SingleOrDefault(); if (element != null) { var attr = element.Attribute("FName"); attr.Value = "Changed"; } doc1.Save(filePath);
Edit: Descendants("AnswerSet") finds the response items, Elements ("Answer") finds the response items,
Where(x => x.Attribute("FName") != null && x.Attribute("FName").Value == "test").SingleOrDefault();
finds an element that contains the FName attribute, and the attribute value is test , SingleOrDefault in the latter, says that you should have only one such element. You can also change it (just call ToList() ) to find all the related elements and, finally, in the if changes the value of the element. Also at the end we save it again with the changed values.
This language (linq2xml) is too simple, and functions such as Descendant and Elements , most of all use the full functions in it, so there is no need to have special knowledge that you can just find in many problems, knowing these functions.
source share