0

我一直在努力阅读一个文本文件并将文本组织成每个项目的块。我需要帮助找出在程序使用过程中可以保存和调用数据的方法。我正在尝试将输出作为数据网格视图。我不知道如何解决这个问题。我想我可以为每个新块创建一个数据表,但我不知道该怎么做。

到目前为止我的代码:

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..

任何和所有的帮助将不胜感激:)

4

1 回答 1

1

可以在此处找到示例帖子,下面概述了关键步骤:

  1. 使用 xml 保存数据
  2. 从 xml 读取数据到列表中
  3. 然后将其绑定到您的网格视图。

示例代码可能是:

[Serializable]
class SomeProperty
{
    public int Count1 {get;set;}   //2550
    public int Count2 {get;set;}   //995
    public int Count3 {get;set;}   //200000
    public int Count4 {get;set;}   //7
}

List<SomeProperty> objects=new List<SomeProperty>();
objects.Add(...)

//Saving
XmlSerializer x = new XmlSerializer(objects.GetType());
using (FileStream stream = System.IO.File.Create(FilePath))
{
    XmlWriter writer = XmlWriter.Create(stream);
    x.Serialize(writer, objects);
}

//Reading
XmlSerializer x = new XmlSerializer(typeof(List<SomeProperty>));
using (FileStream fs = new FileStream(FilePath, FileMode.Open))
{
    XmlReader reader = new XmlTextReader(fs);
    var objects= (List<SomeProperty>)x.Deserialize(reader);
}
于 2013-04-25T05:29:22.893 回答