2014-05-12 28 views
2

我有一组需要读取的分隔文本文件,创建一个类和对象,并在其中存储成员。我是一个初学者,只是希望指出正确的方向。任何帮助将不胜感激。非常感谢你。C#将文本文件保存到对象中的最佳方式

我做了一个类与对象:

public string left; 
public string right; 

和我的表单代码:

private void button1_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog of = new OpenFileDialog(); 
    of.ShowDialog(); 
    textBox1.Text = of.FileName; 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    StreamReader sr = new StreamReader(textBox1.Text); 
    textBox2.Text = sr.ReadToEnd(); 
    // sr.Close(); 
} 

private void button3_Click(object sender, EventArgs e) 
{ 
    string[] split1 = textBox2.Text.Split(';'); 
    foreach (string segment in split1) 
    { 
     //split sub-segment 
     string[] split2 = segment.Split(':'); 
     //check if it's valid 
     if (split2.Count().Equals(2)) 
     { 
      id textfile = new id(); 
      textfile.left += // ???? 
      id textfile1 = new id(); 
      textfile.right += // ???? 
+0

什么语言?!? –

+0

我不清楚你到底在问什么。 – displayname

+0

对不起c#。忘了补充说明 – user3617347

回答

2

一般来说,它要最好使用JSONXML将数据保存到文本文件,而不是分隔文字或自定义格式。这是因为我的语言提供了良好的JSON和XML支持,并且易于使用。

public class MyCustomClass //this class will hold your data 
{ 
    public string Left {get; set;} 
    public string Right {get;set;} 
} 

MyCustomClass mcc=new MyCustomClass(); //create an instance of your class 
mcc.Left="yes"; //set some properties 
mcc.Right="nope"; 
string json=JsonConvert.SerializeObject(mcc); //convert to JSON string 
File.WriteAllText("mcc.txt",json); //save to file 

//later on, when you want to read it back from the file 
string json=File.ReadAllText("mcc.text"); //read from file into a string 
MyCustomClass mcc=JsonConvert.DeserializeObject<MyCustomClass>(json); //convert the string back to an instance of MyCustomClass 

上面,我们使用Json.NET它可为.NET Framework(available on NuGet)库。我们用它将我们的对象转换为一个字符串(serialize),然后再将它转换回一个对象(反序列化)。请注意,为了使用JsonConvert类,您需要Json.NET引用,并在类using Newtonsoft.Json;的顶部添加using语句。

1

阅读分隔文件很常见,并且有很多方法可以用于该主题。我个人使用一个streamReader文件中读取和拆分它的分隔符:

Foo foo = new Foo(); // custom class 
string file = "export.CSV"; 
if (System.IO.File.Exists(file)) 
{    
     // Do work 
     using (var reader = new StreamReader(file)) 
     { 
      while (!reader.EndOfStream) 
      { 
       // split on the delimeter 
       var readLine = reader.ReadLine(); 
       if (readLine == null) continue; 
       var lines = readLine.Split(new[] { ',' }); 

       foreach (string s in lines) 
       { 
        // do something with the data from the line 

       } 
       // alternatively, you could specify your objects if your files 
       // layout never changes. Just be careful to catch the exceptions! 
       foo.bar = lines[0]; 
       foo.baz = lines[1]; 

      } 
     } 
} 
+0

如果有内部逗号会发生什么? – mason

+0

我唯一想念的就是存储每个分割的值,这就是我所挂起的东西。我不想显示它只是将对象存储为列表 – user3617347

+0

@ user3617347检查我的最后编辑 –

2

你要寻找的是serialization。当你有一个已知的结构(如你的班级string leftstring right),你想把这个结构写到一个文本文件中。之后,您想要重新读取这些信息,并自动使用每个值填充该类。正如梅森指出的那样,JSON设置起来相当容易。您可以创建所需的类结构,并告诉JSON将其保存到指定的文件中(通过SerializeObject)。

由于.NET允许reflection,因此JSON可以将文本文件重新转换为class的内容,而无需手动'myClass.left = [some_value_from_json]'。

个人而言,我会使用JSON或XML,因为命名您的数据块意味着它更具可读性,并且您的解析器能够处理某人重新排列数据(文件定义无关紧要left,然后它定义right)。如果您重新安排.CSV文件,则会导致数据损坏。

相关问题