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.
Related
Effectively, I want to take an XML string, utilize the power of Tag Helpers representing the XML tags, and have it render the XML.
I can make Tag Helpers easily enough to generate what I want, but I can't seem to find a way to render an XML string as Razor so the Tag Helpers will work.
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.
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
Well I am using DIFFPlex for comparing two html code fetching from db but i want to ignore html tags and their styling (only content), is there any way to achieve this.
I am waiting for your answer guys.
Thanks
I would suggest using the HtmlAgilityPack to strip all the html tags.
See the following question: https://stackoverflow.com/a/16875574/12919
I have a substring what contains HTML tag and I need to shorten it but display it with the same formatting as it appears on the string.
It doesn't have to be exactly X characters long, but it should be short enough to be displayed inside a panel with a certain width and height?
Is there any way I can achieve this using c#?
What about using CSS? I.e. displaying the panel with a fixed height regardless of its content?
Thanks..
Example: I have the following panel containing a label that contains text with html tags:
I need to remove the scroll bar without making the panel longer but keeping this height & this width..
If you have following html code:
<div class="div1"> Some Really Bold String </div>
You can provide css to hide the scroll bars,
.div { overflow:hidden; height:200px; width:200px;}
height and width values are just for example purpose.
overflow:hidden does not let the content of the div to expand out side the div.
you will find more information on overflow here.
You could use a regex to find the contents of the specific tag. Use a .substring to shorten the result afterwards.
A example could be:
<h1>head</h1>
<p>contents</p>
Regex could be:
<p\b[^>]*>(.*?)</p>
Result would be:
<p>contents</p>
Now just exclude the start and end tag. as its a fixed length.
I found more interesting reading about changing the content between HTML tags. Take a read here (regex ftw!):
http://www.thatsquality.com/articles/how-to-match-and-replace-content-between-two-html-tags-using-regular-expressions
Another solution that might not drive you as crazy if you want to solve it in c#:
HTML Agility Pack
Take a look at the examples part of the site. Great little tool!