none
C# - How to convert text file to formatted datatable in c# RRS feed

  • Question

  • Hi,

    I want to convert text file in to datatable which is required particular highlighted value.

    The above text file, i just want to convert into datatable.

    i want to fetch only highlighted square values and remove duplicates then stored in to datatable.

    Note : I need pnly highlighted portion data and want to remove remaining all..

    Ho to get those data as datatable..?

    Monday, December 11, 2017 6:13 AM

Answers

  • Hi Gani,

    I would like to provide some hints to you.

    1. You may extract the portion you need with the Substring method and store the matched one into a List<String>

    2. You could use the Distinct method to remove the duplicated ones in your list.

    List<String> noDupes = yourList.Distinct().ToList();

    3. You may use the following method to convert your List<String> into DataTable

    public static DataTable ToDataTable<T>(List<T> items)
    {
            DataTable dataTable = new DataTable(typeof(T).Name);
    
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
               var values = new object[Props.Length];
               for (int i = 0; i < Props.Length; i++)
               {
                   values[i] = Props[i].GetValue(item, null);
               }
               dataTable.Rows.Add(values);
          }
          
          return dataTable;
    }

    Hope this is helpful to you.


    Ouch Liu , Developer ≈ Designer


    • Edited by Ouch Liu Monday, December 11, 2017 10:54 AM
    • Marked as answer by Gani tpt Monday, December 11, 2017 1:11 PM
    • Unmarked as answer by Gani tpt Tuesday, December 12, 2017 6:38 AM
    • Marked as answer by Gani tpt Tuesday, December 12, 2017 10:09 AM
    Monday, December 11, 2017 10:53 AM
  • I mocked up some of the text file (), enough to show a concept that may work as is but may need tweaking. In this case the text file is in the same location as the executable, adjust as needed.

    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==> 1 :C:\adadad
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==> EMPLOYEE NO FOUND IN DATABASE
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==> A_E001-A-H
    20/5/2016 01:02:14 PM ==> A_E002-A-H
    20/5/2016 01:02:14 PM ==> A_E003-A-H
    20/5/2016 01:02:14 PM ==> A_E004-A-H
    20/5/2016 01:02:14 PM ==> A_E001-A-H
    20/5/2016 01:02:14 PM ==> A_E008-A-H
    20/5/2016 01:02:14 PM ==> A_E001-A-H
    

    Code, the apostrophes are to see we only got what we came for, otherwise they are not needed.

    using System;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // read all lines
                var contents = File
                    .ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt"));
    
                // find out where to start
                var result = contents
                    .Select((line, index) => new { Line = line, Index = index })
                    .Where(item => item.Line.Contains("EMPLOYEE NO FOUND IN DATABASE"))
                    .FirstOrDefault();
    
                // jump down past dashes and get data
                var data = contents
                    .Skip(result.Index + 2)
                    .Where(line => !string.IsNullOrWhiteSpace(line))
                    .Select(line => line.Substring(26))
                    .Distinct();
    
                // show results
                foreach (var item in data)
                {
                    Console.WriteLine($"'{item}'");
                }
    
                // next place data into a container
            }
        }
    }

    Results

    'A_E001-A-H'
    'A_E002-A-H'
    'A_E003-A-H'
    'A_E004-A-H'
    'A_E008-A-H'


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by KareninstructorMVP, Moderator Monday, December 11, 2017 12:00 PM
    • Marked as answer by Gani tpt Monday, December 11, 2017 1:11 PM
    • Unmarked as answer by Gani tpt Tuesday, December 12, 2017 6:38 AM
    • Marked as answer by Gani tpt Tuesday, December 12, 2017 10:09 AM
    Monday, December 11, 2017 11:59 AM
    Moderator

All replies

  • Hi Gani,

    I would like to provide some hints to you.

    1. You may extract the portion you need with the Substring method and store the matched one into a List<String>

    2. You could use the Distinct method to remove the duplicated ones in your list.

    List<String> noDupes = yourList.Distinct().ToList();

    3. You may use the following method to convert your List<String> into DataTable

    public static DataTable ToDataTable<T>(List<T> items)
    {
            DataTable dataTable = new DataTable(typeof(T).Name);
    
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
               var values = new object[Props.Length];
               for (int i = 0; i < Props.Length; i++)
               {
                   values[i] = Props[i].GetValue(item, null);
               }
               dataTable.Rows.Add(values);
          }
          
          return dataTable;
    }

    Hope this is helpful to you.


    Ouch Liu , Developer ≈ Designer


    • Edited by Ouch Liu Monday, December 11, 2017 10:54 AM
    • Marked as answer by Gani tpt Monday, December 11, 2017 1:11 PM
    • Unmarked as answer by Gani tpt Tuesday, December 12, 2017 6:38 AM
    • Marked as answer by Gani tpt Tuesday, December 12, 2017 10:09 AM
    Monday, December 11, 2017 10:53 AM
  • How to read particular elements of the data in the text file...?
    Monday, December 11, 2017 10:57 AM
  • I mocked up some of the text file (), enough to show a concept that may work as is but may need tweaking. In this case the text file is in the same location as the executable, adjust as needed.

    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==> 1 :C:\adadad
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==>
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==> EMPLOYEE NO FOUND IN DATABASE
    20/5/2016 01:02:14 PM ==> ---------------------------------------------------------
    20/5/2016 01:02:14 PM ==> A_E001-A-H
    20/5/2016 01:02:14 PM ==> A_E002-A-H
    20/5/2016 01:02:14 PM ==> A_E003-A-H
    20/5/2016 01:02:14 PM ==> A_E004-A-H
    20/5/2016 01:02:14 PM ==> A_E001-A-H
    20/5/2016 01:02:14 PM ==> A_E008-A-H
    20/5/2016 01:02:14 PM ==> A_E001-A-H
    

    Code, the apostrophes are to see we only got what we came for, otherwise they are not needed.

    using System;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // read all lines
                var contents = File
                    .ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt"));
    
                // find out where to start
                var result = contents
                    .Select((line, index) => new { Line = line, Index = index })
                    .Where(item => item.Line.Contains("EMPLOYEE NO FOUND IN DATABASE"))
                    .FirstOrDefault();
    
                // jump down past dashes and get data
                var data = contents
                    .Skip(result.Index + 2)
                    .Where(line => !string.IsNullOrWhiteSpace(line))
                    .Select(line => line.Substring(26))
                    .Distinct();
    
                // show results
                foreach (var item in data)
                {
                    Console.WriteLine($"'{item}'");
                }
    
                // next place data into a container
            }
        }
    }

    Results

    'A_E001-A-H'
    'A_E002-A-H'
    'A_E003-A-H'
    'A_E004-A-H'
    'A_E008-A-H'


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by KareninstructorMVP, Moderator Monday, December 11, 2017 12:00 PM
    • Marked as answer by Gani tpt Monday, December 11, 2017 1:11 PM
    • Unmarked as answer by Gani tpt Tuesday, December 12, 2017 6:38 AM
    • Marked as answer by Gani tpt Tuesday, December 12, 2017 10:09 AM
    Monday, December 11, 2017 11:59 AM
    Moderator
  • Excellent. This what i  want...
    Monday, December 11, 2017 1:11 PM
  • Thanks for your support for removing duplicates..
    Monday, December 11, 2017 1:12 PM
  • The logic is correct and perfect.

    But, i am getting error while reading a text file.

    the text file stored in temp folder.


    anything need to modify the code below..

    var contents = File
                    .ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, txtfilePath));

    Error : The process cannot access the file 'C:\Users\P0010\AppData\Local\Temp\Report_Log.txt' because it is being used by another process.

    Pls. find attached error.


    • Edited by Gani tpt Tuesday, December 12, 2017 6:19 AM
    Tuesday, December 12, 2017 6:18 AM
  • Hi,

    According to the error message you get:

    Error : The process cannot access the file 'C:\Users\P0010\AppData\Local\Temp\Report_Log.txt' because it is being used by another process.

    It seems your text file is using by another process.

    Maybe you could try to find if there is any executing instances of your application running in the Task Manager, stop them all, and retry.

    Or, it might be helpful to restart your computer and have another test.

    Finally, please remember to release your text fine once you no longer keep it opened.


    Ouch Liu , Developer ≈ Designer

    Tuesday, December 12, 2017 7:48 AM
  • Thanks.


    I thing, i am writing a text file in same folder and reading a text file same folder.

    is there any issue writing and reading same folder or same file....?

    why i am using appdata temp file, it will automatically delete file End of the day. we are generating many log files. for the reference purpose only we are using appdata temp folder.

    But, C:\\temp ==> it is manually edit file.

    that's why...?


    when i use  var contents = File.ReadLines(@"C:\Users\P0010\AppData\Local\Temp\Report_Log.txt");  ==> getting error (Path I).




    • Edited by Gani tpt Tuesday, December 12, 2017 9:37 AM
    Tuesday, December 12, 2017 8:32 AM
  • Thank you all.

    i tried different solution for the same  code.

    1. copied same file to another folder.

     2. read the file from copied folder.

    3. after done all the process, delete the file in copied folder.


    it's working.

    Thanks to  Kareninstructor...kudoos...
    Tuesday, December 12, 2017 10:09 AM