Manipulate XML files in c# I just finished my working code, but still want to improve it.

I want to transform this input:

&lt;item&gt;asdf&lt;/item&gt;
&lt;item&gt;asdf&lt;/item&gt;
&lt;item&gt;asdf&lt;/item&gt;


to this output:

&lt;string name="x1"&gt;asdf&lt;/string&gt;
&lt;string name="x2"&gt;asdf&lt;/string&gt;
&lt;string name="x3"&gt;asdf&lt;/string&gt;


My current code:

   static void Main(string[] args)
        {
            String content;

            using (StreamReader reader = new StreamReader("arrays2.xml"))
            {
                content = reader.ReadToEnd();
            }

            int counter = 0;
            int startIndex = 0;
            while ((startIndex = content.IndexOf("&lt;item&gt;", startIndex)) != -1)
            {
                counter++;           
                content = content.Substring(0, startIndex) + "&lt;string name=\"rule" + counter + "\"&gt;" + content.Substring(startIndex + 6);
            }

            content = content.Replace("&lt;/item&gt;", "&lt;/string&gt;");


            using (StreamWriter writer = new StreamWriter("arrays2_formatted.xml"))
            {
                writer.Write(content);
            }
            Console.WriteLine(content);


        }


I think the bottleneck is in the content assignment within the loop, as a lot of String instance are created and thrown away.

However, is there a completely different way to do this efficiently?
Don't parse it through string manipulation, use the XML libraries in the framework.  I'd recommend LINQ to XML.  Parse it and make your changes.

var xmlStr = @"&lt;root&gt;
&lt;item&gt;asdf&lt;/item&gt;
&lt;item&gt;asdf&lt;/item&gt;
&lt;item&gt;asdf&lt;/item&gt;
&lt;/root&gt;";

var doc = XDocument.Parse(xmlStr);         // parse from string
//var doc = XDocument.Load("arrays2.xml"); // or load from file
var items = doc.Descendants("item");       // find all item elements
var index = 1;
foreach (var item in items.ToList())  // ToList() required since we're modifying the document
{
    var name = "x" + index++;
    var nameAttr = new XAttribute("name", name);           // create the name attribute
    var value = (string)item;                              // read the value as a string
    var newNode = new XElement("string", nameAttr, value); // create the new element
    item.ReplaceWith(newNode);                             // replace the current one
}
//doc.Save("arrays2_formatted.xml"); // save the changes to the file