I have a simple string variable that contains a portion of HTML inside. For example:
string contents = "<div><p>Hi how are you. Click here if you want to know more";
I want to include this HTML in page:
<div class="description">
#contents
</div>
However, it messes up the rest of the page because of unclosed tags.
Is there a function (or a helper) that reads and formats the HTML inside for example, to complete the HTML without errors:
#Html.DisplayProperHTML(contents)
This will render as:
<div><p>Hi how are you. Click here if you want to know more</p></div>
There is no such functionality built-in.
You can use the HTML Agility Pack to parse and fix broken HTML.
Related
I have a string like
"For more information, go to google"
When I directly put this string inside a span in my razor (cshtml) view, I get the raw string in the DOM. How can I make it behave like HTML content, i.e. text followed by a link? Is there any way other than parsing the string and extracting the href and link text and then putting them inside a new anchor tag?
I want to get first-level elements via parsing HTML file with HTML Agility Pack ,for example result will be like this:
<html>
<body>
<div class="header">....</div>
<div class="main">.....</div>
<div class="right">...</div>
<div class="left">....</div>
<div class="footer">...</div>
</body>
</html>
That each is contains other tag...
I want to extract all text that exist in the website,but separately . for example right side separate,left side separate , footer and so...
can anyone help me?
thanks...
Use HtmlAgilityPack to load the webpage from the given URL, then parse it by selecting the correct corresponding tags.
HtmlWeb page = new HtmlWeb();
HtmlDocument doc = new HtmlDocument();
docc = page.Load("http://www.google.com");
If you want to select a specific div with the class name 'header', you do so by using the DocumentNode property of your document object.
string mainText = doc.DocumentNode.SelectSingleNode("//div[#class=\"main\"]").InnerText;
Chances are though that you have several tags in your HTML that are members of the 'main' class, thus you have to select them all then iterate over the collection, or be more precise when you select your single node.
To get a collection representation of all tags i.e. in class 'main', you use the DocumentNode.SelectNodes property instead.
I suggest you take a look at this question at SO where some of the basics and links to tutorials are available.
How to use HTML Agility pack
I want to parse razor view file in c# . I have also used Html Agility Pack to parse razor view file but it failed to save correct file contents.
Basically i want to change some html elements inner html by server side using c#
<div id="content1">
<p>this contents i want to change </p>
<span>contes</span>
</div>
i want to change content1 inner html by c# like this
<div id="content1">
<span>#Function.gethtml()</span>
</div>
I have used html agility pack to change inner html contents but it is not parsing razor syntax function like
#Url.Content("abc.css") and other function
For more information
I have created a project in asp.net mvc3 for shoes and garments shops.Now i want to change css and html dynamically.i want to extract inner html from any html template file where element id will be "content1" and place this inner html in cshtml file where element id is "content1
Simply i want razor file parser that can parse both html and razor syntax like html parser
Is there any way to parse razor view file and it html elements.
Any Other solution
You want to access / modify razor content using server side content.
But MVC is a different way of building websites/applications to webforms.
There are now server side controls or webcontrols. You can use JQuery to do same.
I just need to find a regular expression for the following:
I have some content in div tag, that includes lot of anchor links in it. So my task is to find anchor links with href as format of "components/showdoc.aspx?docid=" And then add onclick event for that anchor link only, leave the rest of the anchor links.
<div id="content" runat="server">
test doc
</div>
This expression gives and add target to it.
RegEx.Replace(inputString, "<(a)([^>]+)>", "<$1 target=""_blank""$2>")
Thanks
If you are looking to make permanent changes to your HTML file, first manage your HTML parsing by loading it into a System.Windows.Forms.WebBrowser control. From there you can perform DOM-like modifications to the HTML without the dangerous repercussions of parsing corruption that can be caused by performing Regex.Replace on the raw file. (Apparently RegEx + HTML is a serious issue for some).
So first in your code you would:
WebBrowser myBrowser = new WebBrowser();
myBrowser.URL = #"C:\MyPath\MyFile.HTML";
HtmlElement myDocBody = myBrowser.Document.Body;
Then you can navigate through your document body, seeking out your div tag and looking for your anchor tags by using the HtmlElement.Id property and HtmlElement.GetAttribute method.
Note: feel free to still use RegEx matching on the URL strings but only after extracting them from a GetAttribute("href") method.
To add the onClick method, simply invoke the HtmlElement.SetAttribute method.
When you have finished all your modifications, save the changes by writing the WebBrowser.DocumentText to file.
Here is a reference:
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.aspx
Don't use regex to parse html, it's evil.
You could use the HTML Agility Pack, it even has a nice NuGet Package.
Alternatively, you could do this on the client side with a single line of jQuery:
$('a[href*="components/showdoc.aspx?docid="]').on('click', myClickFunction);
This is making use of the Attribute Contains Selector.
If you want to find the docid in your click function, you could write something like this in your click function:
function myClickFunction(e){
var href = $(this).attr('href');
var docId = href.split('=')[1];
alert(docId);
}
Note that this assumes there's only ever one query string value, if you wanted to make this more robust you could do something like in this answer: https://stackoverflow.com/a/1171731/21200
Iam using wkhtmltopdf for html to pdf conversion of an html page using C# code and it is working absolutely perfect but i want to convert a particular part of html page to pdf like by specifying div id of that part or any similar method.How can i do this?
Please help
You can just create a new HTML page with just your div as a body:
<html>
<head><title>...</title></head>
<body>
<!-- PLACE YOUR DIV HERE -->
</body>
</html>
Since you already know how to produce a PDF from an HTML file, you know know how to produce a PDF from a DIV ;)
BUT: Of course the CSS is going to be all wonky, since the selectors won't match anymore etc. This is a problem for the general case, but I'm guessing in your specific case, you can make that work.