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.
onebite2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.