I have a HTML code which I need to store in a string but the html code is very large how do I do it?This problem came of now where and I don't know much about C# by searching on the net I found code to convert an html code to PDF but before that I need to store it in a string. I need guidance on this.
-
2I really doubt your html code is large enough to exceed possible limit of string length in C#. Even in x86 environment and old .NET versions limit of size for any .NET object was around 2Gb - this should be pretty enough for any html code used in reality. So what is your **real** problem? – Andrey Korneyev Sep 11 '15 at 06:57
-
is the html code in a file ? – Nikita Shrivastava Sep 11 '15 at 07:10
6 Answers
In addition to Rahul's method, you can also use an @
symbol in front of the string to escape the entire thing.
string str = "<span id=\"somevalue\">"
can also be written as:
string str = @"<span id=""somevalue"">"

- 515
- 3
- 12
There are ways to add (HTML) text to your C# code, as others have suggested. However, if the text is very large, this becomes difficult to manage.
Why not store the HTML in a separate file and use File.ReadAllText to read it into your program? That way you can edit that HTML source as real HTML instead of a string literal.

- 38,117
- 9
- 79
- 111
-
-
@FrankTheTank - an upvote shows to other readers that you found an answer useful ... – Hans Kesting Mar 14 '18 at 07:28
You can store the HTML code in your string simply by putting it inside the quotes. The only thing which you have to keep in mind is that you need to "escape" the quotes in your strings wherever you find them in your HTML code.
Something like:
string str = "<span id=\"somevalue\">"
The backslash \
will escape the ""
in your HTML code.
As Andy commented the theoretical limit for storing the string is of 2GB which is approximately 2,147,483,647 characters but you can do more to that practically.

- 168,305
- 31
- 280
- 331
-
Well, 2Gb technically is not always 2,147,483,647 characters since strings can be unicode encoded. So in worst case it is half of that value. – Andrey Korneyev Sep 11 '15 at 07:06
-
-
1@RahulTripathi I am so late for the party, is it even still on? Nevermind. But the `backslash` comment on your answer has just saved me tenfold. Thank you. – davvv Dec 09 '17 at 15:45
Putting large strings in code is not very nice in my point of view.
I like to embedd a file as resource in an assembly. This resource can be retrieved very easy. This makes your code much more readable.
Take a look at: How to read embedded resource text file. This is much better than storing it directly in the source code.
EDIT
If you want to render your asp.net code to html, take a look at: ASP.NET how to Render a control to HTML?
-
It doesn't looks that OP wants to place some string in code. I guess he tries to grab some html from web and convert to pdf (since pdf conversion was mentioned). – Andrey Korneyev Sep 11 '15 at 07:05
-
1Oh, i thought he has a kind of static template he need in his code. Let's wait for some explanation from the OP :) – BendEg Sep 11 '15 at 07:06
-
@AndyKorneyev The thing is i have an aspx page in my project..I want to convert that page in pdf hence i need to write the html content of that page to astring but the content is like 700 to 800 lines...so is there any easy method to do this – darshan kodkany Sep 11 '15 at 09:14
-
@darshankodkany At which point of the asp.net engine would you like to grab the html code? I added sample link for you. – BendEg Sep 11 '15 at 09:15
-
@BendEg i have an aspx page which is designed in html can u suggest step by step procedure to convert that page to pdf – darshan kodkany Sep 11 '15 at 09:24
-
Mayber you could wrapt you page in a kind of `Printerable-Control` and use the method i've postet (link)? This would be a acceptable solution. Or not? – BendEg Sep 11 '15 at 11:16
While other answers are completely true, but adding \ before each " is slightly hard.
Another option that you can consider is to store it in a Setting Property, When you write in setting property designer or paste your value there, behind the scene it puts \ wherever needed and you don't need to do anything else.
Also Resource Editor do the same for you.

- 120,393
- 18
- 203
- 398
declare a variable of datatype string, string szHTML =""; The entire HTML can be pasted inside. szHTML = string.Format(@" ")

- 34
- 4
-
1Not quite: you usually have quotes (`"`) inside that html which need to be escaped. – Hans Kesting Sep 11 '15 at 07:25