[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

Reading Text file in with c# code.

Reading Text file in with C# code
Hi all looking for some information on this.  I have a text file that has sections all the way through that look like this:

dn: uid=joeshmoe,
objectclass: myuser
uid: joeshmoe
creatorsname: cn=Directory Manager
createtimestamp: 20020827204754Z
usertype: testuser
admin: 0

I need to read through the file and when I hit the first line in this file then I need to look for things such as "uid" and store it to a variable.  Also on the first line I need to capture the ou=444444 into a variable named CustNO.    Then I want to store all these to variables.  Once I hit a new start line with "uid" in it i want to write to a SQL database the variables into a record I have all that figured out.  Then I want to clear the vars and then start over.  Like admin I will need to store to a variable but I dont need the creatorsname etc.  Thanks all for any help.  One last thing one of the lines is called "custid:" but then there is also colons in the value.  I need to read into a variable after the first colon so for example,
custid: 34:45:89.


I used a business object class to declare all these variables.
0
onebite2
Asked:
onebite2
  • 8
  • 2
1 Solution
 
silemoneCommented:
use scanner class....

if they all have carriage line feeds, then let the delimiter be the natural delimiter which should separate by \r\n

then split the text  at :  
0
 
silemoneCommented:
oops..this is c#...i said scanner class which is java...
0
 
silemoneCommented:
so use split twice...

split first at "\r\n" or Environment.Newline

then split text at : to get value...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
silemoneCommented:
here's an example of splitting to parse...

http://msdn.microsoft.com/en-us/library/ms228388(VS.80).aspx
0
 
SRigneyCommented:
If you don't want to split then read line by line, find the first colon and use everything to the left of that as your key and to the right as your value.
0
 
onebite2Author Commented:
Thanks for ur suggestions....I have no clue how would I implement this clearly ....can any one help me write an example.
Attached is the textfile I have.which contains :

Date:May 13, 2008
address1:StONE RAYMOND
address2,
state,
zip,
fax,
body of the letter,:
 The person listed above has filed an unemployment claim against your company. Based
  on the state's ruling, the  has filed an appeal. A hearing will be scheduled
  by the state within the next four to six weeks.
 
            * The claimant's file
            * Documentation (exhibits)
            * Hearing procedures (what to expect at the hearing)
            
  Please review the "Hearing Witness Guide" for helpful preparation information for
  the upcoming hearing latest ap headlines Longoria is AL Rookie of Year, Soto wins NL
  awardEU to resume frozen partnership talks with Russi Sex charge dropped against ex-Texas
  police chief Lottery results .DHL to cut 9,500 jobs and close US service centers
 
specialist name.:  Sam.


Now I want to read the text from this file ,just the variable values line by line and insert them into a table in database.
I am sure first I need to read the text and then load the values with adhoc SQL...

Can any one give me the sample code for this scenario....
Thanks!!!I appreciate ur help.
0
 
silemoneCommented:
will start working on it...by the way, this is c# yes?
0
 
onebite2Author Commented:
Thanks Silemone,

Yes it is c#.I appreciate your help.
0
 
silemoneCommented:
wow...that middle section is really getting me...but i am working on this...
0
 
silemoneCommented:
This code works for the template you provided...Stores each heading into a separate variable...i then send it to a label named label3 to show that it works...



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;


namespace fileReader
{
     public partial class Form1 : Form
     {
         
          public Form1()
          {
               InitializeComponent();
               string strLine=String.Empty;
               string strLine2 = String.Empty;
               StreamReader objSR = null;
                try
                {
                     objSR = new StreamReader(new FileStream(@"c:\test.txt", FileMode.Open));
                     string[] separator = { "Date:", "address1:", "address2:", "state:", "zip:", "fax:", "body of the letter:", "specialist name:" };
                                   
                     strLine = objSR.ReadToEnd();
               
                     string[] strParts = strLine.Split(separator, StringSplitOptions.None);

                     int x = strParts.Length;

                     string date = strParts[0];
                     string address1 = strParts[1];
                     string address2 = strParts[2];
                     string state = strParts[3];
                     string zip = strParts[4];
                     string fax = strParts[5];
                     string body = strParts[6] + strParts[7];
                     string specialist = strParts[8];




                          label3.Text = date + Environment.NewLine + address1 + Environment.NewLine + address2 + Environment.NewLine + state + Environment.NewLine + zip + Environment.NewLine + fax + Environment.NewLine + body + Environment.NewLine + specialist;

                }
                catch (Exception ex)
                {
                     string error = ex.StackTrace.ToString();
                }
                finally
                {
                     if (objSR != null)
                     {
                          objSR.Close();
                          objSR.Dispose();
                     }
                     
                }
          }

          private void Form1_Load(object sender, EventArgs e)
          {

          }
     }
}
0
 
silemoneCommented:
With the code, if you have only the heading that you say, then it searches by heading (headings are the delimiters)...With those headings, there will always be 9 fields (for some reason, the last paragraph of body is given a separate field...if you need to add heading, etc...then play with code and change it accordingly to accommodate template...
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now