473,799 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parse text file and export to xml file using C#

1 New Member
Hi there,

I have a text file that does not have any delimiters - it is simply assumed that I know how long each field is and they are always in the same order.
(i.e. JOHN DOE 12345606JUN0720 07JUL2009 34 PXP)

Using C# I need to:
1. Grab the information in the text file.
2. Parse it out by grabbing the different "fields" (5th to 9th char = name, 24th to 27th is product etc).
3. Sort the fields into the order I want (i.e. move the dates to the beginning, then the last name, then first name then product etc)
4. Export the result to an XML file.

Is there a Sample of C# code out there or a tutorial ANYWHERE that will show me how this is done? I'm at my wits end.
Thank you for any help!
Gwenna
Jun 29 '06 #1
3 18724
Tzumer
2 New Member
This is classic for the use of regex ( Regular Expressions )
do you need code samples for that?
Jul 6 '06 #2
Egypt_81
25 New Member
this code works i did it for u, try it and tell me if u need more help

private void parse()
{
string sLine, sName, sProduct, sDate;

//initialize reader from ur existing text file ex. datafile.txt
// IF UR TEXT FILE IN NOT ON THE bin/debug use the full path
StreamReader sr = new StreamReader("d atafile.txt");
//grab the first in a string
sLine = sr.ReadLine();

//cut the string into the parts u want as name, date,...
sName = sLine.Substring (5,3);
sDate = sLine.Substring (15,18);
sProduct = sLine.Substring (24,3);
sr.Close();

StreamWriter sw = new StreamWriter("o utputfile.txt") ; //enter the full path for the output file
//write the substrings u have now in an output file
sw.Write(sDate+ " ");
sw.Write(sName+ " ");
sw.Write(sProdu ct+" ");
sw.Close();
}
Jul 6 '06 #3
Egypt_81
25 New Member
Don't forget to use
using System.IO;

regards,
Hesham el ebrashy
Jul 6 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
12007
by: John M. Lembo | last post by:
I am using Perl to parse a text file and output to another file. The text file has data on Unix virtual memory (vmstat) and I want to delete lines that I don't want and output lines that I want to a new file. The script I have is giving me blank lines in the new file. #!/usr/local/bin/perl #program to read systemdata file #and write to systemstats file # #
6
6106
by: nate | last post by:
Hello, Does anyone know where I can find an ASP server side script written in JavaScript to parse text fields from a form method='POST' using enctype='multipart/form-data'? I'd also like it to parse the filename. <form name='form1' method='POST' enctype='multipart/form-data' action='sub.asp'> <input type='text' name='title1' value='value1'> <input type='file' name='file1'>
3
2063
by: Roel Schoonveld | last post by:
In Access 97 I made an application that makes text files that are exported. The names of the files contain two variables: 1. the value of the field BRIN in a table VasteGegevens of my database. 2. the sequence number. So for instance, the export file may be called Bron02AP003.txt, where Bron is a fixed text, BRIN has the value 02AP and it is the third file of this kind.
2
8318
by: Raghu Gupta | last post by:
Hi, Actually i am facing some problem using quickbooks and presently i am not so expert in using it. I am using c# code to retrive the data from quickbooks. it doesn't matter which language we are using. So i have .iif file of employee list,so through code i have to read .iif file and get the data out of it and store that data in database. As i can use QuickBook only to import .iif file and from quickbook i can
5
1856
by: Ash Phillips | last post by:
Ok, I'm not sure how I can explain this but here goes. I have this program that uses a ListView for entries. What I have the program doing is taking all the items from the listview, and writing them to an HTML document. Well what I did in VB6 was get all the info from the listview in a loop routine and write the HTML into a text box. When that was completed I then exported all the text from that text box to an html file.
3
6015
by: hbarnett | last post by:
I am in process of converting Access 97 databases to Access 2003. Some of the Access 97 databases export data to a text file with a non standard file extension, such as "ext" or "NAL". When I try to run the export in the converted Access 2003 database, I get the error "Cannot update: Database or object is read-only." It seems that the real problem is that I can only export a text file if I use either "txt", "csv", "tab", or "asc" as...
6
4342
by: Vic4338 | last post by:
I have a database with 50000 records containing phone numbers, I need to export the phone numbers sorted by area code, and create/save to .txt using the area code as the phone number. I create the export template using the wizard but cant get the code to run off my form button. Thanks any help is greatly appreciated.
0
978
by: sterrell | last post by:
I am reading text from a file and I need help on how to pass my array to my parse.. Sample text file is::: H 234 J 345 I 345 K 345 L 456 M 456
1
21870
Colloid Snake
by: Colloid Snake | last post by:
Hey everyone. I'm currently attempting to band-aid an horrible use of a Cacti implementation (and I swear, this is only temporary! No, really! They told me it's in the budget for next year, and has already been approved... I hope...) Anyway... I am using rrdtool to dump the data into an xml file. So this xml file is going to have several hundred (possibly thousand) lines that consist of: <some tag I'm forgetting> <some other tag> datestamp...
0
871
by: dorothywtkhk | last post by:
Dear All, I got a problem with the following case: I would like to enable a button after the file export (using asp.net), the code is: Dim fi As System.IO.FileInfo = New System.IO.FileInfo(strPath) Response.Clear() Response.AddHeader("Content-Disposition", "attachment;filename=" + fi.Name) Response.AddHeader("Content-Length", fi.Length.ToString()) Response.ContentType = "Text Documents (*.txt)" Response.WriteFile(fi.FullName)
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.