Hack Forums
[C#] New line problem while downloading data from url - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] New line problem while downloading data from url (/showthread.php?tid=5687749)



New line problem while downloading data from url - Dotexe - Aug 6, 2017

Hello, i've problems while downloading data from url. This is my code so far

Code
byte[] raw = wc.DownloadData("url");

            string webData = System.Text.Encoding.UTF8.GetString(raw);
            TextBox9.Text = (webData);

but the problem is that the whole content is on one line, however the textbox is multiline, i've heard i had to do something with regex but not sure what.🤝 in advance.


* - Electron - Aug 6, 2017

It depends on where you download it from. If you download http://site.com/document.txt, it will display it just fine. However, if you download an HTML data, you might have to replace some tags (e.g. p, br) with "\r\n", Environment.NewLine.

Not sure if you understood what I mean

Example:

Code
WebClient wc = new WebClient();
byte[] raw = wc.DownloadData("url");
string webData = Encoding.UTF8.GetString(raw );
TextBox9.Text = webData.Replace("<br />", Environment.NewLine);



* - Dotexe - Aug 6, 2017

Yes it is an html data, and i had to replace these tags u mentioned, but i had no idea how to achieve it, i will do that when i get home and will let you know if works. Thank you !
EDIT: not working with this as well ;(


* - Owl - Aug 6, 2017

Depending on how much markup there is and what you want to extract, it may be worthwhile using a parser such as htmlagilitypack.

Also, download directly as a string.


* - Electron - Aug 6, 2017

Can you give a sample from the input html.

Code
TextBox9.Text = Regex.Replace(webData, "<.*?>", string.Empty);

This will remove all of the tags, but since you want to add new lines, this is not gonna work for you because you have to check which tag makes it go on a new line and then replace that tag with \r\n or Environment.NewLine. Because the new line may be coming by <p>some text</p>, some text<br />, div tag, etc.


* - RickSanchez - Aug 6, 2017

(Aug 6, 2017 01:41 AM)Dotexe1 Wrote:  Hello, i've problems while downloading data from url. This is my code so far

Code
byte[] raw = wc.DownloadData("url");

            string webData = System.Text.Encoding.UTF8.GetString(raw);
            TextBox9.Text = (webData);

but the problem is that the whole content is on one line, however the textbox is multiline, i've heard i had to do something with regex but not sure what.🤝 in advance.

Can you show the website/markup and what you are trying to extract and format, please?


* - Dotexe - Aug 6, 2017

It is an raw, and has <p> i guess thats why i tried with \r\n but the same result, i will try with ur 2nd method

(Aug 6, 2017 05:46 AM)RickSanchez Wrote:  Can you show the website/markup and what you are trying to extract and format, please?

It is https://hastebin.com/raw/itofofibug


* - RickSanchez - Aug 6, 2017

(Aug 6, 2017 05:47 AM)Dotexe1 Wrote:  It is an raw, and has <p> i guess thats why i tried with \r\n but the same result, i will try with ur 2nd method

If you give url I can write the code, otherwise it's just guessing what exactly you're trying to achieve...


* - Dotexe - Aug 6, 2017

(Aug 6, 2017 05:50 AM)RickSanchez Wrote:  If you give url I can write the code, otherwise it's just guessing what exactly you're trying to achieve...

Already did it, https://hastebin.com/raw/itofofibug , its not something special, just test each line


* - RickSanchez - Aug 6, 2017

(Aug 6, 2017 05:51 AM)Dotexe1 Wrote:  Already did it, , its not something special, just test each line

Sorry op.

Anyway, what I see in this doc is

Test1
Test2
Test3


* - Dotexe - Aug 6, 2017

I tho thats the thing you wanted to see right ?


* - RickSanchez - Aug 6, 2017

(Aug 6, 2017 06:05 AM)Dotexe1 Wrote:  I tho thats the thing you wanted to see right ?

The webpage you are parsing, the entire html markup of the page, or just give link to the page instead.

hastebin has some issues with the ssl btw, and I need to load it in wepbroxy...


* - Electron - Aug 6, 2017

Code
TextBox9.Text = webData.Replace("\n", Environment.NewLine);

There you go


* - Dotexe - Aug 6, 2017

(Aug 6, 2017 06:11 AM)1912 Wrote:  
Code
TextBox9.Text = webData.Replace("\n", Environment.NewLine);

There you go

Thank you, i tried with \r\n but havent with only \n , now it works, thanks to everyone else :)