尝试拆分制表符分隔列表并创建对象列表c#

时间:2018-03-14 00:29:56

标签: c#

我有一个文件上传,它接收一个制表符分隔列表,我试图使用标题作为属性拆分成员工对象列表。我已经尝试将列表按行和制表符拆分为对象,但是我没有得到正确的对象列表。

以下是标签分隔列表的示例:

domainName名称accountname givenname surname email physicaldeliveryOffice

" CN = Fred Smith,OU =销售,OU = MRO,OU =用户,OU = Owasso,OU =测试,DC =测试,DC = com" Fred Smith Fred.Smith Fred Smith Fred.smith@test.com office"
" CN = John Smith,OU = Sales,OU = MRO,OU = Users,OU = Miramar,OU = test,DC = test,DC = com" John Smith John.Smith John Smith John.smith@test.com office"

我想要的是创建一个使用列作为属性的员工列表..

这是我迄今为止所做的尝试:

员工类

 public class Employee
{
    public string DomainName { get; set; }
    public string Name { get; set; }
    public string SamAccountName { get; set; }
    public string GivenName { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string PhysicalDeliveryOfficeName { get; set; }
}

读取列表并将其解析为对象的代码:

            FileInfo file = new FileInfo(Server.MapPath("~/MyFiles/" + fileName));
            string[] delimitedByLine = System.IO.File.ReadAllText(file.FullName).Split(new string[] { "\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);

            var employeesList = delimitedByLine.Select(x => new Employee
            {
                DomainName = x[0].ToString(),
                Name = x[1].ToString(),
                AccountName = x[2].ToString(),
                GivenName = x[3].ToString(),
                Surname = x[4].ToString(),
                Email = x[5].ToString(),
                PhysicalDeliveryOfficeName = x[6].ToString()


            }).ToList();

            return employeesList;

更新

经过一些帮助后,我可以做些什么才能让它发挥作用:

public ActionResult Parse(string fileName)

    {
        try

        {
            FileInfo file = new FileInfo(Server.MapPath("~/MyFiles/" + fileName));
            string[] delimitedByLine = System.IO.File.ReadAllText(file.FullName).Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToArray();

            var employeeList = delimitedByLine.Select(x =>
            {
                string[] delimitedByTab = x.Split(new string[] { "\t" }, StringSplitOptions.None);
                return new Employee()
                {
                    DomainName = delimitedByTab[0].Replace("\"", ""),
                    Name = delimitedByTab[1],
                    SamAccountName = delimitedByTab[2],
                    GivenName = delimitedByTab[3],
                    Surname =  delimitedByTab[4],
                    Email = delimitedByTab[5],
                    PhysicalDeliveryOfficeName = delimitedByTab[6]
                };
            }).ToList();
            return PartialView("~/Views/Shared/_Employees", employeeList);
        }

        catch (Exception ex)

        {
            throw ex;
        }


    }

1 个答案:

答案 0 :(得分:2)

您需要在每行中拆分字符串。

var employeeList = delimitedByLine.Select(x=>
{
    string[] delimitedByComma = x.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    return new Employee()
    {
       DomainName = delimitedByComma[0],
       Name = delimitedByComma[1],
       AccountName = delimitedByComma[2],
       GivenName = delimitedByComma[3],
       Surname = delimitedByComma[4],
       Email = delimitedByComma[5],
       PhysicalDeliveryOfficeName = delimitedByComma[6]
    }
}).ToList();;