Solved

Reading the file from specific line to specific line

Posted on 2013-12-30
5
268 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
[X]
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
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 36

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

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.

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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