从c#.net中的文本文件中提取文本 [英] extracting text from a text file in c#.net

查看:76
本文介绍了从c#.net中的文本文件中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下格式的文本文件

I have a text file in the following format

name : designation : salary :


我想通过将名称作为输入来提取信息.

我想将文本文件转换为xml文件,然后以表格形式使用它.

谁能向我建议C#.net


I would like to extract the info of by taking name as input.

I want to convert the text file to xml file and then use it in form.

Can any one suggest me the procedure in C#.net

推荐答案

中的过程,如果您有文本文件,则可以简单地使用 ^ ]

您可以创建一个具有以下变量的类:名称,名称和薪水作为属性.

然后是一个Foreach方法,并用char:"分割传入的字符串.

将项目存储在您创建的类的新实例中,然后将其添加到您的类的列表中.
IF you have a text file you could simply use ReadAllLines[^]

You could make a class that had the variables: name, designation and salary as properties.

Then a Foreach method and split the incoming string by ths char ":".

Stor the items in a new instance of the class you made, and add this to a List(of yourclass).


假设您的文本文件可能包含这些条目中的几个,如下所示:
Assuming your textfile could have several of these entries and looks like:
name : TestEntry designation : Target1 salary : 2345


您可以编写以下代码:


You could write this code:

private void Form1_Load(object sender, EventArgs e)
{
    string[] lines = File.ReadAllLines(@"C:\Temp\Test.txt");
    foreach( string str in lines )
    {
        string[] parts = str.Split(':');
        if( parts.Length == 4 )
        {
            string name = parts[1].Substring(0, parts[0].IndexOf(' ') + 1).Remove(0,1);
            parts[2] = parts[2].Remove(0, 1);
            string designation = parts[2].Substring(0, parts[2].IndexOf(' ') + 1);
            string salary = parts[3].Remove(0, 1);
            MessageBox.Show("Name: " + name + " Designation: " + designation + " Salary: " + salary);
        }
    }
}


这篇关于从c#.net中的文本文件中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆