I'm trying to split a string from a file and to do some transformations around it. I'm working on ASP.NET framework in C#. I know, there could be better ways of storing this type of information than this .txt file, but a conversion is not planned right now.
The .txt content looks like this:
//comment lines
//have to be ignored
1.0.1 [
# Foo 01/01/2001
- Text
- Text
- ...
# Bar 02/02/2002
- Text
...
]
1.0.2 [
...
]
...
This is what I tried:
protected void Page_Load(object sender, EventArgs e)
{
LoadText();
}
protected void LoadText(){
//get file 'update.txt' from server
string path = Server.MapPath("update.txt");
//read file removing all lines that start with '//' or empty lines
string[] cleanTextArray = System.IO.File.ReadAllLines(path).Where(line => !line.StartsWith("//") && !string.IsNullOrEmpty(line)).ToArray();
//merge 'lines' into a single string
string text = string.Join("\n", cleanTextArray);
//split text in versions on ']'
string[] versions = text.Split(']');
//for each version
foreach(string version in versions)
{
//split version in lines on '['
string[] lines = version.Split('[');
Div.InnerHtml += string.Format("<h3>{0}</h3>", lines[0]);
string changes = lines[1];
//split changes on "#"
string[] changesArray = changes.Split('#');
foreach(string change in changesArray)
{
string[] changeArray = change.Split('-');
string[] changeTitle = changeArray[0].Split(' ');
string changeAuthor = changeTitle[0];
string changeDate = changeTitle[1];
Div.InnerHtml += string.Format("<p><b>{0}</b> on {1}</p><ul>", changeAuthor, changeDate);
for(int i = 1; i < changeArray.Length; i++)
{
Div.InnerHtml += string.Format("<li><p>{0}</p></li>", changeArray[i]);
}
Div.InnerHtml += "</ul>";
}
}
}
Everytime I run it, the page throws an 'IndexOutOfRangeException'. The neat part is that if I cycle through each array and print the result, everything works fine...
What can I do? Thanks in advance to everyone.