Using xml linq :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string xml = @"<Root>
<Tag1>
<USA></USA>
<UK></UK>
</Tag1>
<Tag2>
<FRA></FRA>
<USA></USA>
</Tag2>
</Root>";
XDocument doc = XDocument.Parse(xml);
List<XElement> tags = doc.Descendants().Where(x => x.Name.LocalName.StartsWith("Tag")).ToList();
List<string> countries = tags.SelectMany(x => x.Elements().Select(y => y.Name.LocalName)).Distinct().OrderBy(x => x).ToList();
for (int i = 0; i < tags.Count; i++)
{
List<XElement> children = countries.Select((x, j) => new XElement(x, "var" + (i + 1).ToString() + ((char)(((byte)'a') + j)).ToString())).ToList();
tags[i].ReplaceWith(new XElement(tags[i].Name.LocalName, children));
}
}
}
}
solved Replace text inside .txt file or .xml file using loop [closed]