I just finished my working code, but still want to improve it.
I want to transform this input:
<item>asdf</item>
<item>asdf</item>
<item>asdf</item>
to this output:
<string name="x1">asdf</string>
<string name="x2">asdf</string>
<string name="x3">asdf</string>
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("<item>", startIndex)) != -1)
{
counter++;
content = content.Substring(0, startIndex) + "<string name=\"rule" + counter + "\">" + content.Substring(startIndex + 6);
}
content = content.Replace("</item>", "</string>");
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?