I have a table where in one column, I have saved all the html of a page. I want to fetch a div( and its contents) from that div using htmlagility how can I do this. I don't want to load it from url or do screen scraping.
// Load your html
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
// Find div with an id or you could use a class if you want
var nodes = htmlDocument.DocumentNode.SelectNodes("//div[#id='myDivId']");
I found this solution.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(#html);
HtmlNodeCollection tableRows = doc.DocumentNode.SelectNodes("//tr");
string content = "";
if (tableRows.Count > 1)
{
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[#class='account-detail']");
content = node.InnerHtml;
}
thank you all for your time.
Related
I am trying to display images from directory , all images are loading but there are not showing , just a box of size i defined through coding .
First here is my code.
string imagepath = Server.MapPath("~/Images/");
string[] images =Directory.GetFiles(imagepath,"*.png",SearchOption.TopDirectoryOnly);
foreach (string filepath in images)
{
System.Web.UI.WebControls.Image te = new System.Web.UI.WebControls.Image();
te.ImageUrl = filepath;
te.Height = 100;
te.Width = 200;
//Here myimages is my div in which all images will be added.
myimages.Controls.Add(te);
}
Now the screen shot what i am getting .
link below:
http://pages.apunkashaher.com/error.html
So, any one can help out what is missing ?
string imagepath = Server.MapPath("~/Images/");
string[] images =Directory.GetFiles(imagepath,"*.png",SearchOption.TopDirectoryOnly);
foreach (string filepath in images)
{
Image te = new Image();
string fileurl = Path.GetFileName(filepath.ToString());
te.ImageUrl = "~/Images/" + fileurl;
te.Height = 100;
te.Width = 200;
te.CssClass = "zoom";
//Here myimages is my div in which all images will be added.
myimages.Controls.Add(te);
}
Hence Solved & Working.
I'd check what links the images are actually rendering in HTML by viewing the source, I suspect that the code is finding the images but that you are not rendering the correct URL for the browser.
In our ASP.NET MVC 3 We are currently generating thumbnail images on the fly with the following code:
public void GetImageThumbnail(string imageName)
{
WebImage wbImage = new WebImage("~/assets/images/gallery/"+imageName+".jpg");
int width = 220;
wbImage.Resize(width, (int)((double)wbImage.Height * width / wbImage.Width));
wbImage.FileName = imageName+"_small.jpg";
wbImage.Write();
}
and displaying them in the view like this:
<img src="#Url.Action("GetImageThumbnail", new {imageName = "motherboard"})" alt="" />
How can we control the image size on the view and generate the complete <img> tag from scratch? We need to specify the image dimension and obtain an <img> tag the contains the width and height properties.
Thanks.
My first thought is to create your own HTML helper method which does all of the work. Something you would consume like:
#Html.ThumbnailImg("motherboard")
That method does the thumbnail work and can output the full desired markup. More info: http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs. For what it's worth, I prefer the extension method design. It seems more palatable to other devs.
UPDATE:
There is a little bit of chicken-and-egg that I didn't think about before. Perhaps something like:
public static MvcHtmlString ThumbnailImg(
this HtmlHelper html,
UrlHelper url,
String imageName,
String elementID,
String altText)
{
var img = new TagBuilder("img");
if (String.IsNullOrEmpty(elementID) == false) img.MergeAttribute("id", elementID);
if (String.IsNullOrEmpty(altText) == false) img.MergeAttribute("alt", altText);
// Generate and cache thumnail here, determining height and width
// ...
var src = url.Action("GetImageThumbnail", new { imageName = imageName });
img.MergeAttribute("height", height);
img.MergeAttribute("width", width);
img.MergeAttribute("src", src);
return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
}
In itextsharp, i use
var paragraph = new Paragraph();
var reader = new StringReader(text);
var handler = new HtmlHandler();
XMLWorkerHelper.GetInstance().ParseXHtml(handler, reader);
foreach (var element in handler.elements)
{
paragraph.Add(element);
}
to get the IElements of a given HTML text "text" since
iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList (reader, stylesheet)
is deprecated and I had some problem with unorderded lists (the list items were not indented and had not bullet).
Is there a possibility to include a css-file (like in the old version of the parser)?
Thanks in advance!
In the current version of the API, ParseXHtml has an overload for passing in a css file.
I'm trying to have an asp.net custom control with a field like:
<ErrorTemplate>
<h1>Error</h1>
<p>Blue wizard shot the food</p>
</ErrorTemplate>
What I want to end up with is:
<h1>Error</h1><p>Blue wizard shot the food</p> in a string in my codebehind.
At the moment, the process I use to get this data out of the template is:
First this gets instantiated into a placeholder by my code:
ErrorTemplate.InstantiateIn(errorPHolder);
which is an asp.net placeholder control - it has to be instantiated into something which can support controls.
What I then want to do is add this to my page using JQuery and a variable, like this:
string script = "var Errortemplate = " + errorPHolder.ToString() + ";";
scriptmanager.register(script); // pseudocode
so the result would be, on my page:
var Errortemplate = '<h1>Error</h1><p>Blue wizard shot the food</p>';
Then I can use JQuery to do someDiv.html(Errortemplate);
Finally resulting in whatever they put in <ErrorTemplate> appearing on the page
Because I'm using JQuery it might be possible to do this a different way, such as adding the placeholder to the page into a hidden div and using JQuery to copy the values out.
Any ideas?
Have you tried to render your control?
StringBuilder sb = new StringBuilder();
using (StringWriter tw = new StringWriter(sb))
{
using (HtmlTextWriter hw = new HtmlTextWriter(tw))
{
errorPHolder.RenderControl(hw);
}
}
// its HTML string representation of errorPHolder
string html = sb.ToString();
I know it's strictly OK to put <script> tags in the body, but in the interest of neatness I'd like to use System.Web.UI.ClientScriptManager to register a script in the <head> of my page. Is there a method to accomplish this?
Thanks in advance.
In these cases I usually add a ContentPlaceHolder in the tag of my Master Page.
Alternately I've used a method (usually in a utility class or PageBase class) that puts the script string in a List and stores it in the ASP.Net Context like so:
List<string> javaScriptUrls = new List<string>();
url = url.ToLower();
javaScriptUrls = Context.Items[JS_KEY] as List<string>;
if (javaScriptUrls == null)
{
javaScriptUrls = new List<string>();
javaScriptUrls.Add(url);
}
else
{
if (!javaScriptUrls.Contains(url))
javaScriptUrls.Add(url);
}
Context.Items[JS_KEY] = javaScriptUrls;
Then OnPreRender of the MasterPage, it reads this List from the Context and builds tags in the header.
List<string> _javaScript = Context.Items[JS_KEY] as List<string>;
foreach (string js in _javaScript)
{
HtmlGenericControl script = new HtmlGenericControl();
script.TagName = "script";
script.Attributes.Add("type", "text/javascript");
if (js.StartsWith("~/"))
script.Attributes.Add("src", head.ResolveUrl(js));
else
script.Attributes.Add("src", js);
head.Controls.Add(script);
AddHeaderLineBreak(head);
}