Your code gave me the expected results when I tested it. I order to try it, I threw together a little test in a console app. I used the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<user>
<given-names>A.B.</given-names>
</user>
<user>
<given-names>Y.Z.</given-names>
</user>
</root>
Then I created a fresh console application project and threw 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());
}
}
It produced the desired output:
<root>
<user>
<given-names>A. B.</given-names>
</user>
<user>
<given-names>Y. Z.</given-names>
</user>
</root>
There must be something else causing the problem here. What sort of environment are you working in? How are you converting the XDocument to a string for output?