我一直在努力阅读一个文本文件并将文本组织成每个项目的块。我需要帮助找出在程序使用过程中可以保存和调用数据的方法。我正在尝试将输出作为数据网格视图。我不知道如何解决这个问题。我想我可以为每个新块创建一个数据表,但我不知道该怎么做。
到目前为止我的代码:
public void findBlocks()
{
String fileName = "Drops.de";
StreamReader streamReader = new StreamReader(fileName);
String npcName;
string npcID;
string itemID;
string itemAmount;
string itemRarity;
string itemName;
while(!streamReader.EndOfStream){
string line = streamReader.ReadLine();
//start of block
if(line.StartsWith("[")){
npcName = line.TrimStart('[');
npcName = npcName.TrimEnd(']');
while(!line.StartsWith("[/")){
line = streamReader.ReadLine();
String[] s = line.Split(' ');
npcID = (s[0]);
itemName = (s[1]);
itemID = (s[2]);
itemAmount = (s[3]);
itemRarity = (s[4]);
DataTable dt = new DataTable();
DataGridViewRow dgv = new DataGridViewRow();
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells[0].Value = itemName;
row.Cells[1].Value = itemID;
row.Cells[2].Value = itemAmount;
row.Cells[3].Value = itemRarity;
dataGridView1.Rows.Add(row);
dataGridView1.Refresh();
line = streamReader.ReadLine();
//MessageBox.Show(npcName);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
search();
}
public void loadItemList(string file)
{
String fileName = file;
StreamReader streamReader = new StreamReader(fileName);
while (!streamReader.EndOfStream)
{
string currentLine = streamReader.ReadLine();
string s;
String id;
String itemName = "";
if (currentLine.Contains("item"))
{
s = currentLine.TrimStart('i', 't', 'e', 'm', ' ', '=', ' ');
id = s.Split('.')[0];
itemName = id.Split('\t')[1];
id = id.Split('\t')[0];
listBox1.Items.Add(itemName + " " + id);
}
else
{
streamReader.ReadLine();
}
}
streamReader.Close();
}
这是我尝试排序的文本文件的示例:
[item1]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item1]
[item2]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item2]
[item3]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item3]
ETC..
任何和所有的帮助将不胜感激:)