Solved

Reading the file from specific line to specific line

Posted on 2013-12-30
5
266 Views
Last Modified: 2014-01-02
I want to get a substring between <body> to </body>

Dim Files As String() = System.IO.Directory.GetFiles("c:\temp\temp1")
            Dim readpages As New ReadingPages()
            For Each sFile As String In Files
                If readpages.IsaspxFile(sFile) Then
                    Dim fileCont As String = System.IO.File.ReadAllText(sFile)
                    'it found something
                    If fileCont.Contains("<body>") = True Then
                     'I want to read the contents from <body> to </body>
                    End If
                End If
               
            Next

Open in new window


Thanks
0
Comment
Question by:KavyaVS
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 170 total points
ID: 39746854
Try:
...

        If fileCont.Contains("<body>") = True Then

            Dim startOffset = fileCont.IndexOf("<body>") + 5
            Dim endOffset = fileCont.IndexOf("</body>")

            Dim body As String = fileCont.Substring(startOffset, endOffset - startOffset)
            Debug.WriteLine(body)

        End If
...

Open in new window

0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 165 total points
ID: 39747036
See my answer at:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28327525.html#a39747030

It seems to be a duplicate question here. Any way my answer compared to Carl's and your original code has the following improvements:
1) Get Files method only retrieves aspx files: (System.IO.Directory.GetFiles("c:\temp\temp1", "*.aspx")
)
2) The body tag is only searched once using index. (No need for fileCont.Contains)
0
 
LVL 4

Assisted Solution

by:ItWorked
ItWorked earned 165 total points
ID: 39748005
why don't you try with
String.Split(String[],StringOptions)

Open in new window

method ?

may be like this...
String [] sepraters=new String [] {"<body>","</body>"};

String HTML=System.IO.ReadAllText("file");
var values=HTML.split(sepraters,StringSplitOptions.RemoveEmptyEntries);
// values[0] = will be first part before <body>
// values[1] = middle part between <body>,</body>
// values[2] = rest of string till end

Open in new window

0
 

Author Closing Comment

by:KavyaVS
ID: 39751647
Thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

831 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