C# ASP.NET: How do I Turn Text into HTML - c#

How would i show the output as HTML. I have tried HTML Decode and it still didn't work.
#section Grid {
#Server.HtmlDecode(lister.gen(new System.IO.StreamReader(Server.MapPath("~/Grid.xml")).ReadToEnd()))
}
Edit: I am taking XML from output.InnerXml (A XMLDocument) and trying to put it into a HTML Document as HTML (As in <a> is a link and <img> is a picture and not Text)

It turns out I had to add #Html.Raw along with HtmlDecode for it to display correctly
#Html.Raw(Server.HtmlDecode(lister.gen(new System.IO.StreamReader(Server.MapPath("~/Grid.xml")).ReadToEnd())))

If you want to show the HTML in an HTML page, you need to use HTML encode, not decode. This will put the proper tags in to turn < and > (and other HTML elements.
ADDED due to comment:
If showing XML AS HTML is the goal, then you will end up extracting the XML, from the DOM and putting it into the format you desire. You can bind XML to a table if you are simply trying to get it into a grid. If you need sorting, etc, LINQ to XML works nicely.
ADDED - second edit
Your XML appears to be XHTML, so you can simply throw it into the stream at the correct location. I would create a server control and then Response.Write the XML from the server control. You will want the decoded version based on what you posted. I was assuming you wanted to show the XML in the page, which was incorrect.
There is one minor bit of an issue with the XML, as it contains paragraph and div tags inside of an anchor tag. Not illegal, but not necessary in this case.

Related

Check if HTML contains anything visible

I want to check in c# if an html document is "visibly empty". Checking InnerHtml is not enough, because the HTML could contain an image or an empty table.
Are there any efficent ways to check a HTML document if there is anything, that translate to something that is not a whitespace?
The complicated way i'm thinking of is removing every html/body/p/br Tag, whitespaces and nbsp items, and checking if anything is left

Hide part of text temporarily, show after user clicks certain element

I'm making a detail page about certain items.
This detail page can contain large blocks of text, and the customer would like to only show the first 100 letters and then put a " ... more " at the end.
When the user clicks this " ... more " the rest of the text can be shown.
Biggest problem: the text is currently is a CMS and has large varieties. Some is pure text, some have html elements in them ...
I tried to cut off the text and put them in spans. Then i could show/hide these spans as i please. The issue here is that there can be a starting element of a certain tag in the first span and the closing element can be in the second span. This causes the DOM hierarchyto be faulty and the result is never pretty.
Does anyone know a ( other ) way to achieve this or a library i can use ?
To be able to extract "readable" characters you need to get the content into a plain text format (get rid of the mark-up).
Since the content is stored in a cms it is likely that the content is structured to be well formed - thus xhtml.
If that is the case you can treat the content as XML. Get the root node and get the innertext property there-of. Then you will have plain text - no tags - and can easily cut it after the first 100 characters or whatever the requirement is.
Hopefully the content doesn't contain js/css!
Edit:
It seems that the markup must be retained.
Try the following xsl to transform and truncate the content:
https://gist.github.com/allen/65817

C# simple html editor - how to search and replace content

Today i'm working on simple html editor in visual c#.
My goal is to open pure html file from local drive (Opendialog load into string or load into webbrowser completed) and allow to edit key fragments.
application should find specific divs and return the full content of that div to textbox or better to combobox (compare to combobox item and show it).
if i change the textbox (or pick up another item from combobox) application should present changes on webbrowser control.
Then i need to print this html as is seen on webbrowser control.
Last thing is to save modified hmtl overwrting original and adding comment with changes at bottom of the html file.
I want to know how to perform search&replace in this project? How to "adress" content of a div?
Better is string searching, indexof, string.replace etc. Or drown into DOM-thing (i don't know both at the moment).
How to present changes on html preview on webcontrol component? And finally overwrite a file?
Code's examples appreciated :)
Thanks in advance
You should use HtmlAgilityPack for that, it makes working with DOM a lot more easier, than parsing it yourself.
http://htmlagilitypack.codeplex.com/
For example, here is how to get all divs
var elements = hdoc.DocumentNode.Descendants("div") /.Where(.your conditions..))/;

c# - how can I read a specific line from a HTML page

I have a HTML page with 2 lines. The 2 lines are regarding an update ,but this update can be downloaded only by a specific user.
Basically the HTML file contains:
beast
1.11
the 1st line is the user and the 2nd is the version.There are no HTML headers or other lines.
Now my problem is,that I really have no idea how to read just the 2nd line
A text document can be easily manipulated by:
File.ReadLines("Text.txt").ElementAt(1);
Is there a similar command wich can be used for reading an HTML file line by line?
HUGE thanks for any reply!!!!
You can use that same exact code to read HTML.
HTML files are ordinary text files that happen to contain HTML tags.
If you want to parse the HTML tags, use HTML Agility Pack (on NuGet).

AntiXss and my desired results

After scrubbing my field with AntiXss.HtmlEncode is there a way to remove all the html elements because they still show up as literals in the display?
No, there isn't. If you want to remove HTML elements and then HtmlEncode the result, you have to actually parse the code and remove the elements.
You can use the HTML Agility Pack or any other HTML parser to parse the content, get the InnerText of the root element (this will be only the text of the content) and then call AntiXss.HtmlEncode on that result.

Categories

Resources