5 Replies - 3996 Views - Last Post: 05 November 2009 - 12:23 PM Rate Topic: -----

#1 Pablo3728  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 13-October 09

Convert file.html to string asp.net vb.net

Post icon  Posted 04 November 2009 - 07:04 AM

Hello, Well I am doing an asp.net application (vb.net), where i upload files. Well, one of this files are html, and I want to convert the file in a string, becouse I want to modify one thing in the html. FOr example, in the html I know that there is the next line: <PABLO> and i wanto to replace it for the word "hello".

I have the code that upload the file, but I couldn't find the code for convert the html. Can you help me?

THank you! :)

Pablo

Is This A Good Question/Topic? 0
  • +

Replies To: Convert file.html to string asp.net vb.net

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Convert file.html to string asp.net vb.net

Posted 04 November 2009 - 08:24 AM

HTML is just text, there is not need to convert anything. Just use the String methods to find and replace the the text you need to modify.

Use the StringReader to open the HTML file and read the text into a String variable.
Was This Post Helpful? 0
  • +
  • -

#3 Pablo3728  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 13-October 09

Re: Convert file.html to string asp.net vb.net

Posted 04 November 2009 - 09:05 AM

HI! Thank you por your reply, ok, i understand now that html is a text, then, I have tu put de file.html in the line :

Dim sr As StreamReader = New StreamReader("FILE.html")

 Public Shared Sub Main()
		Try
			' Create an instance of StreamReader to read from a file.
			Dim sr As StreamReader = New StreamReader("TestFile.txt")
			Dim line As String
			' Read and display the lines from the file until the end 
			' of the file is reached.
			Do
				line = sr.ReadLine()
				Console.WriteLine(Line)
			Loop Until line Is Nothing
			sr.Close()
		Catch E As Exception
			' Let the user know what went wrong.
			Console.WriteLine("The file could not be read:")
			Console.WriteLine(E.Message)
		End Try
	End Sub



the text of the file.html, is save in the variable "line" ?
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Convert file.html to string asp.net vb.net

Posted 04 November 2009 - 09:54 AM

Use the ReadToEnd method to load the HTML into a String and yes, the text will be in the 'line' variable.

 Public Shared Sub Main()
		Try
			' Create an instance of StreamReader to read from a file.
			Dim sr As StreamReader = New StreamReader("TestFile.txt")
			Dim line As String
			' Read and display the lines from the file until the end
			' of the file is reached.
			line = sr.ReadToEnd()

			sr.Close()
		Catch E As Exception
			' Let the user know what went wrong.
			Console.WriteLine("The file could not be read:")
			Console.WriteLine(E.Message)
		End Try
	End Sub


Was This Post Helpful? 1
  • +
  • -

#5 Pablo3728  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 13-October 09

Re: Convert file.html to string asp.net vb.net

Posted 05 November 2009 - 05:18 AM

Thank you Jayman, now i have de html in the variable "line", but If i want to modify one line of the string, how can i say?.
Example:

This is the html in the string "line":
<-------->
<-------->
----
---
<EVIMED>
<----->
<----->

I want to replace the line <EVIMED> for a new word, for example "pablo". Later than modifiy the html will be:

<-------->
<-------->
----
---
pablo
<----->
<----->

EXist a class or something to do this kind of things in visual basic¿?

THank you!!!

This post has been edited by Pablo3728: 05 November 2009 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Convert file.html to string asp.net vb.net

Posted 05 November 2009 - 12:23 PM

The String class has everything you need, use the Replace method. One thing to remember is that the Replace method will replace every instance of the word. So if you have '<EVIMED>' in the string more than once, all of them will be replaced with 'pablo'.

line = line.Replace("<EVIMED>", "pablo")

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1