Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to parse the contents in a text file using vb.net

Posted on 2004-04-21
15
399 Views
Last Modified: 2010-05-18
I have a text file as follows:

<Task>  Testing for task </Task>

<TaskDetails> Testing for task details.This
can span multiple lines
like this. </TaskDetails>

<SubTask> Testing for SubTask. This can also
span mutliple lines</SubTask>

<SubTaskDetails> Sometimes there can be space like empty lines which must be ignored. This can happen between tasks or subtasks.</SubTaskDetails>

<SubTask> This is another subtask with
lines

between

lines
Andall the lines between must be preserved. </SubTask>

<SubTaskDetails>
The line after the last subtask and the start of this subtaskdetails however must be ignored.
</SubTaskDetails>

<SubTask>  Another subtask </SubTask>
<SubTaskDetails> Sub Task Details go here </SubTaskDetails>

<SubTask> Subtasks can have tags in separate lines as well.
</SubTask>

<SubTaskDetails> There can be a < or > in the body of hte text and they must be processed correctly.
Subtask details can also be multiple
lines like this.
</SubTaskDetails>

<Task>
Anotehr task
</Task>
<TaskDetails>
Task details for this task go here
</TaskDetails>

<SubTask> This is a subtask that belongs to this task and not the previous task.
</SubTask>

<SubTaskDetails>
Details of the subtask go here. THere can be lot more subtasks for this task.
</SubTaskDetails>

I want to parse the contents (ie between tags ) and populate in a text fileds. how to do this in vb.net only
and file is a text file only and not xml or not html file
thanks
0
Comment
Question by:anu2004
  • 9
  • 3
  • 2
  • +1
15 Comments
 
LVL 6

Expert Comment

by:zulu_11
ID: 10877239
I believe the text you are talking about is actually an XML and if that is the case then the best method would be to use the XML Namespace..

Zulu
0
 

Author Comment

by:anu2004
ID: 10885346
absolutely not xml it is only txt file.
 thanks
 
0
 
LVL 6

Expert Comment

by:zulu_11
ID: 10885420
could you post you file here...

zulu
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:anu2004
ID: 10885910
how to post it?
<Task>  Testing for task </Task>

<TaskDetails> Testing for task details.This
can span multiple lines
like this. </TaskDetails>

<SubTask> Testing for SubTask. This can also
span mutliple lines</SubTask>

<SubTaskDetails> Sometimes there can be space like empty lines which must be ignored. This can happen between tasks or subtasks.</SubTaskDetails>

<SubTask> This is another subtask with
lines

between

lines
Andall the lines between must be preserved. </SubTask>

<SubTaskDetails>
The line after the last subtask and the start of this subtaskdetails however must be ignored.
</SubTaskDetails>

<SubTask>  Another subtask </SubTask>
<SubTaskDetails> Sub Task Details go here </SubTaskDetails>

<SubTask> Subtasks can have tags in separate lines as well.
</SubTask>

<SubTaskDetails> There can be a < or > in the body of hte text and they must be processed correctly.
Subtask details can also be multiple
lines like this.
</SubTaskDetails>

<Task>
Anotehr task
</Task>
<TaskDetails>
Task details for this task go here
</TaskDetails>

<SubTask> This is a subtask that belongs to this task and not the previous task.
</SubTask>

<SubTaskDetails>
Details of the subtask go here. THere can be lot more subtasks for this task.
</SubTaskDetails>

I want to parse the contents (ie between tags ) and populate in a text fileds. how to do this in vb.net only
and file is a text file only and not xml or not html file
thanks
0
 
LVL 6

Expert Comment

by:zulu_11
ID: 10886046
what i meant was to paste the complete contents of the file that you want to parse and give the details of which all textFields will be filled with what all data...Need to be specific of what kind of functionality you want!!

Zulu
0
 

Author Comment

by:anu2004
ID: 10886165
entire file is that have pasted before.
I want to get contents between <Task></Task> and <TaskDetails></taskDetails> and <subTask></SubTaskDetails> etc.. Place them in  strings so that I will retrive into textboxes
 thanks
0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 10892932
Regular expressions are your best friend.  You need to have at the top of your page this statement:

Imports System.Text.RegularExpressions

Then, in your code, declare a regular expresstion object.

Dim rex As Regex

'if you want to parse multiple delimiters in one line, use a collection
Dim rexColl As MatchCollection

rex = New Regex("[<Task>]")

rexColl = rex.Matches(line) 'this will make a collection of indexes where matches have occured for a single string.

'to extract the data you use a substring, such as the one below to extract everything between 2 matches
data = line.Substring(rexColl.Item(0).Index + 1, rexColl.Item(1).Index - rexColl.Item(0).Index - 1)

'or, in the case of your file, reading line by line

If rex.IsMatch(line) Then
         'do whatever when you find a match
End If

I hope this helps
Majin Loki
0
 

Author Comment

by:anu2004
ID: 10897592
I have used streamreader and when using this
data = line.Substring(rexColl.Item(0).Index + 1, rexColl.Item(1).Index - rexColl.Item(0).Index - 1)

 it is raising "IndexOutOfBounds" Exception

0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 10899469
I'm sorry, I wasn't exactly clear. If you are reading line by line, you need to not use a collection.  A collection is best when you have multiple instances of a certian string that occur at variable intervals on a single line, such as comma delimited or tab delimited.  If you read line by line, at least in your sample, you are not going to have more than one match found.  Use the regular expression and do this test.

If rex.IsMatch(line) Then
         'do whatever when you find a match
End If


you can use another regular expression to test forthe ending clause.  In the example I gave you, we are testing for the <Task> tag.  I'll try to elaborate here a little more to make things more clear.  Most of what I've learned has been through trial and error, so as to why it works or how it works, I am unsure.  I didn't test this code, but you should be able to get the gist from it and do some experimenting yourself.

Dim startRex As Regex
            Dim endRex As Regex
            Dim data As String, line As String

            'create our starting and ending tage regular expressions
            startRex = New Regex("[<Task>]")
            endRex = New Regex("[</Task>]")

            'priming read from the file
            line = oRead.ReadLine()

            'unil EOF
            While oRead.Peek <> -1
                If startRex.IsMatch(line) Then
                    data = line.substring(startRex.Match(line).Index + 6)
                    If endRex.IsMatch(data) Then  'if the ending tag is on the same line
                        data = data.Substring(0, endRex.Match(data).Index)
                    Else 'we must read on.  I will assume that file will contain ending tags always
                        line = oRead.Read()
                        While Not endRex.IsMatch(line)
                            data = data & line
                        End While

                        'if we are outside the loop, we have found an end tag
                        data = data & line.Substring(0, endRex.Match(line).Index)
                    End If
                End If
            End While

If you need any more help or clarification, let me know.

Majin Loki
0
 

Author Comment

by:anu2004
ID: 10906296
Thank you.I will explain my task....
I have a text file as I have posted above.It contain tags<Task></Task>,<TaskDetails></TaskDetails>,<SubTask></SubTask><SubTaskDetails></SubTaskDetails>
these tags are fixed.. all the data will be with these tags only.
file can contain any no of <Task></Task>or<SubTask></SubTask> etc..
until other <Task> tag is found in the file, entire contents belong to first <Task> itself.
so while the reading the file if <Task> tag is encountered,the contents between <Task></Task> will be saved in the data base table named Task (which contain Taskid(autonumber),Task,TaskDetails columns) and the <TaskDetails></TaskDetails> content is saved in the table under same Taskid.
when encountered <SubTask></SubTask> content will be saved in subTask Table(which contain Taskid,SubTaskid(autonumber),SubTask,SubTaskDetails columns)(Tables are related with taskid columns).
 so on..
while the reading the file if another <Task> tag is encountered,the contents between <Task></Task> will be saved in the Task and SubTask table under different TaskId... and SubTaskId  respectively.
A Task can Contain any number of sub tasks..
every sub task of task should be saved with same taskid and different subtaskid 's.

Please let me know how to do it .
thanks




0
 

Author Comment

by:anu2004
ID: 10915274
I want the solution for my question urgent
0
 

Author Comment

by:anu2004
ID: 10946046
close my question ,I have solved the problem myself.
thanks
0
 

Author Comment

by:anu2004
ID: 11002138
close my question ,I have solved the problem myself.
thanks
0
 

Author Comment

by:anu2004
ID: 11002408
delete my question
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 11004988
Closed, 60 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question