On this Url there is text i want to mine
http://www.mefik.co.il/provider.asp?provider_id=10757
I'm looking for the class 'big_obj_px_news_page'
tried all kinds of xpath options.
any help ?
I suggest you install Firefox+Firebug+Firepath to validate your xpaths. Your xpaths were close, but not enough.
//div[#class='big_obj_px_news_page']
// or if this div may have more class names
//div[contains(#class, 'big_obj_px_news_page')]
I created a unit test with the following code:
using System;
using System.IO;
using HtmlAgilityPack;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
namespace HtmlAgilityPackTests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "\\test.html"));
var item = doc.DocumentNode.SelectNodes("//*[contains(#class, 'big_obj_px_news_page')]");
Assert.IsNotNull(item);
}
}
}
This test passes with the exact html on the page provided. In your code you wrote var item = doc.DocumentNode.SelectNodes(Xpath), are you typing the exact xpath string above, or are you trying to use an xpath object?
If you're using an XPath object, it could be that you are setting up your XPath object incorrectly. The only other option I see is that you are not loading your Html correctly. In the unit test code above "test.html" contains the full html source from the page you provided, and resides in the same directory as the c# source code. In the test.html file properties window in Visual Studio, I've set "Copy to Output Directory" to "copy if newer". It's build action is "Content".
Perhaps if you describe how you're loading your html, we can be of further assistance.
Related
I am using RC2 of ASP.Net MVC Core.
I have added my using directives to _ViewImports and it is complaining that the namespace is incorrect. I use the exact same namespace in my controller and it works fine but will not work in the views.
The using directive is referencing a class library in the same solution.
#using xxx.Web
#using xxx.Web.Models
#using xxx.Web.Models.AccountViewModels
#using xxx.Web.Models.ManageViewModels
#using Microsoft.AspNetCore.Identity
#using xxx.yyy
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
I have done significant research on the problem but it looks like it really should be as simple as adding the namespace. It even autocompletes in _ViewImports so I am not misspelling it (I have copied and pasted it just in case).
In case it is relevant I do use a "top Level" namespace so my web project is xxx.Web and my class library is xxx.yyy.
If you have the latest version of ASP.NET Core, this could solve your problem:
services.Configure<RazorViewEngineOptions>(options =>
{
var previous = options.CompilationCallback;
options.CompilationCallback = (context) =>
{
previous?.Invoke(context);
context.Compilation = context.Compilation.AddReferences(MetadataReference.CreateFromFile(typeof(hbulens.MyBucketList.Utilities.EnumUtilities).Assembly.Location));
};
});
or you can try:
services.Configure((RazorViewEngineOptions options) =>
{
var previous = options.CompilationCallback;
options.CompilationCallback = (context) =>
{
previous?.Invoke(context);
context.Compilation = context.Compilation.AddReferences(myAssemblies);
};
});
Put this code in the ConfigureServices method of the startup class.
I ran into this exact same problem, and found this question via Google. I solved it by ensuring that my class library was a .NET Core/Class Library rather than a Windows/Class Library (when using the "Add New Project" dialog). I ended up removing my old class library, creating a new project of the correct type, and adding my source files to it.
could anyone help me with XSL file validation? I don't want to validate it for start/closed tags but for "inside atributes".
For examle:
My XSL file contains line <td><xsl:value-of select="year"/></td>
I need check this one for syntax errors like <td><xsl:vae-of select="year"/></td>.
Is there any way how to do this with javascript or jQuery?
An XSLT processor checks the stylesheet code so in Mozilla browsers you could pass a DOM document with your stylesheet code to the importStylesheet method of an XSLTProcessor object e.g.
var proc = new XSLTProcessor();
var sheet = new DOMParser().parseFromString([
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">',
'<xsl:template match="/"><xsl:vae-of select="year"/></xsl:template>',
'</xsl:stylesheet>'
].join('\n'), 'application/xml');
try {
proc.importStylesheet(sheet);
}
catch(e) {
// handle error here e.g.
console.log(e.message);
}
outputs "Component returned failure code: 0x80600001 [nsIXSLTProcessor.importStylesheet]".
But be aware that an XSLT 1.0 processor is supposed to process stylesheet code with a version greater than 1.0 in forwards compatible mode and that way if you wanted to validate user input code as XSLT 1.0 you would need to make sure that it has version="1.0" to catch any errors against the version 1.0 of the XSLT language.
And of course that error message is not very helpful to identify what's wrong in the code.
I read this [useful article] that says I can create a library of inline helpers by putting them in a view in the special folder App_Code. When I moved my #helper functions there, calls to extension helpers I have stopped working. I read [in this SO article] that there's an issue because the #helpers are static but my extensions are not... I tried the 2 different ways but cannot make it work. It fails to recognise the existence of my extension helpers.
'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Image'
my extension helper is called 'Image'. What should I be looking for?
When you write the Helperextension in razor view.
You need to call it like FileName.Method.
eg you have CustomHelpers.cshtml file in app_code and in that you have a method
#helper TruncateString(string input, int length)
{
if (input.Length <= length)
{
#input
}
else
{
#input.Substring(0, length)<text>...</text>
}
}
you can call it from index.cshtml
#CustomHelpers.TruncateString("Example", 8);
As a matter of example, the following code use the "RenderPage" function within a helper library:
#helper GetSection(string sectionName, string sectionTitle, string sectionFileName){
<div id="#sectionName">
<h1>#sectionTitle</h1>
#System.Web.WebPages.WebPageContext.Current.Page.RenderPage("~/Views/Shared/MySections/" + #sectionFileName)
</div>
}
It demonstrate how to retrieve the "current page" (context sensitive) instance.
This code works fine from within your own "common library" Razor helper file (".cshtml") situated in the "*app_code*" subfolder of your project.
ok, my problem had to do with namespace... so I have in \Views\Shared\HtmlHelpers.cs:
public static class Html
{
public static MvcHtmlString Image(this HtmlHelper helper, string src, object htmlAttrs = null)
{
which I generally access from my pages like this:
#Html.Image("/path/to/image")
in App_Code\Helpers.cshtml:
#helper AddButton(string path)
{
var Html = ((System.Web.Mvc.WebViewPage) WebPageContext.Current.Page).Html;
#Html.Image(path);
}
but Intellisense would underline the "Image" and complain:
'System.Web.Mvc.HtmlHelper<object>' does not contain a definition for 'Image'
and no extension method 'Image' accepting a first argument of type
'System.Web.Mvc.HtmlHelper<object>' could be found (are
you missing a directive or an assembly reference?)
the reason seemed to be that the Helpers.cshtml needs to have #using for the namespace... on my regular pages the namespace is included in my web.config but this page seems exempt from that mechanism
What is the best way to parse html tag like that:
Results
i just need to extract a href value.
I need to do this on controller.
I know i may use linq to xml for example or regex.
Any ideas what is the better way for that case?
May be there is any MVC helpers ready to go?
Basically what i need to do:
I have my extension method witch returning current url
public static string GetCurrentUrl(this ViewContext context)
{
string action = (string)context.Controller.ValueProvider.GetValue("action").RawValue;
string controller = (string)context.Controller.ValueProvider.GetValue("controller").RawValue;
if (action.ToLower() != "index")
return String.Format("/{0}/{1}", controller, action);
else if (action.ToLower() != "index" && controller.ToLower() != "home")
return String.Format("/{0}", controller);
else
return "/";
}
I need to compare this url with the value from a href like that Results
Use a XML parser and avoid regex. For this specific case XDocument seems easy enough:
var doc = XDocument.Parse("Results");
var href = doc.Element("a").Attribute("href").Value;
For more complex scenarios when HTML specific parsing is required and manipulating the DOM you could use HTML Agility Pack.
parsing html is notoriously difficult, there are so many hidden gotchas. The HTML Agility pack, which is a .NET HTML parsing library. Fizzler enables css selectors for html in c# and is built on top of the agility pack.
I've got the following custom html helper in asp.net mvc 3
public static string RegisterJS(this HtmlHelper helper, ScriptLibrary scriptLib)
{
return "<script type=\"text/javascript\"></script>\r\n";
}
The problem is that the result is getting html encoded like so (I had to add spaces to get so to show the result properly:
<script type="text/javascript"></script>
This obviously isn't much help to me.. Nothing I've read says anything about this.. any thoughts on how I can get my real result back?
You're calling the helper in a Razor # block or an ASPX <%: %> block.
These constructs automatically escape their output.
You need to change the helper to return an HtmlString, which will not be escaped:
return new HtmlString("<script ...");