Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Html into a string in C#

Tags:

string

c#

In my project, I need to read some URLs and store the starting tags into some variables, but the project won't compile. May be, its because I am not using the assignment to the string correctly. Following is what i tried and got the compile error

string startTag = "<span id="productLayoutForm:OurPrice" class="pdp_details_hs18Price" itemprop="price">";

string anotherStartTag = "<span class="price final-price our fksk-our" id="fk-mprod-our-id">Rs.<span class="small-font">"

Please tell, what should be the correct code for above and where can I learn how to store such HTMLs into string or how to use string for such puposes.

like image 471
Pankaj Upadhyay Avatar asked Sep 13 '26 19:09

Pankaj Upadhyay


2 Answers

You need to "escape" the quotes in your strings, for example:

string startTag = "<span id=\"productLayoutForm:OurPrice\" class=\"pdp_details_hs18Price\" itemprop=\"price\">";

The \ before the quotes that are inside the string tells the C# compiler that the quotes are part of the string and not the beginning/end of the string.

like image 156
Rob Avatar answered Sep 15 '26 07:09

Rob


The " sign indicates the start and end of a string.so to use it in the middle of a string you have to escape it, do that by setting a backslash in front of it.. Like this: \"

like image 25
f2lollpll Avatar answered Sep 15 '26 08:09

f2lollpll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!