Your code gave me the expected results when I tested it. I order a try, I put together a little test in a console application. I used the following XML:
<?xml version="1.0" encoding="utf-8" ?> <root> <user> <given-names>AB</given-names> </user> <user> <given-names>YZ</given-names> </user> </root>
Then I created a new console application project and dropped this in the program class:
class Program { static void Main(string[] args) { XDocument doc = XDocument.Load("XMLFile1.xml"); foreach (XElement initial in doc.XPathSelectElements("//given-names")) { string v = initial.Value.Replace(".", ". ").TrimEnd(' '); initial.SetValue(v); } Console.WriteLine(doc.ToString()); } }
He produced the desired result:
<root> <user> <given-names>AB</given-names> </user> <user> <given-names>YZ</given-names> </user> </root>
There must be something else causing the problem. What environment do you work in? How do you convert an XDocument to a string for output?
source share