FizzlerEx/HtmlAgilityPack QuerySelectorAll not working - c#

I've been attempting to set up FizzlerEx, found at http://fizzlerex.codeplex.com/. After adding the references to my project, I've attempted to run the example code given on the website- the entirety of my code is listed below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;
namespace Fizzler_Test
{
class Program
{
static void Main(string[] args)
{
var web = new HtmlWeb();
var document = web.Load("http://example.com/page.html");
var page = document.DocumentNode;
foreach (var item in page.QuerySelectorAll("div.item"))
{
var title = item.QuerySelector("h3:not(.share)").InnerText;
var date = DateTime.Parse(item.QuerySelector("span:eq(2)").InnerText);
var description = item.QuerySelector("span:has(b)").InnerHtml;
}
}
}
However, this yields build errors, claiming that:
Error 1 'HtmlAgilityPack.HtmlNode' does not contain a definition for 'QuerySelectorAll' and no extension method 'QuerySelectorAll' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?)
It would seem that QuerySelectorAll is not actually a part of HtmlNode, but given that this is the official example code taken verbatim from the website, I'd expect the creators understand how their library works. I'm at a loss as to what the actual issue could be.
A related problem seems to have been found here, but no suitable answer was ever found: Fizzler and QuerySelectorAll

It would seem that QuerySelectorAll is not actually a part of HtmlNode, but given that this is the official example code taken
verbatim from the website, I'd expect the creators understand how
their library works. I'm at a loss as to what the actual issue could
be.
You are correct about this part. Though you are not correct about the second part, since the author of HAP isn't the author of FizzlerEx. The problem is elsewhere.
Simply by looking at the error, you get the only clue you need to go to solve this .
Error 1 'HtmlAgilityPack.HtmlNode' does not contain a definition for 'QuerySelectorAll' and no extension method 'QuerySelectorAll' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?)
So, what do we get; it tells us that there is no method called QuerySelectorAll in the class HtmlNode in the namespace HtmlAgilityPack. If we take a look at the source code of HAP, you can easily determine that the error message is indeed correct, since there is no method by that name in the class we are looking.
Source code for HtmlAgilityPack.HtmlNode - class
Where is this method that we want to use, but cannot find?
It's here, In the Fizzler.Systems.HtmlAgilityPack.HtmlNodeSelection-class.
After trying a few things, I get the code to work perfectly just as it is. The problem was the extensions in the reference between Fizzler and HAP source code.
If you download Fizzler you get HtmlAgilityPack at the same time. When you add the references in Visual Studio (assuming you use that), only add
Fizzler.Systems.HtmlAgilityPack.dll
HtmlAgilityPack.dll
Clean your solution and rebuild it and it should work!

You should add Fizzler by right clicking on references -> Manage Nuget Package, and search online for it, you will find it as Fizzler for HtmlAgilityPack, and then can download it.

Related

Why use the following statement using aName = library in c#?

I was searching a ZigBee library for c# and I found GBee. I took a look to the code and I've found the following statement:
using AtCmd = NETMF.OpenSource.XBee.Api.Common.AtCmd;
Why is used this statement and what means?
Searching into the code I also found that AtCmd is used as a "class" to send standard command e.g.(AtCmd.RestoreDefaults,AtCmd.NetworkReset)
This meas, that all AtCmd statements in code will become from NETMF.OpenSource.XBee.Api.Common namespace. It simply means 'default namespace' for this type.
Probably you have in code two exact names, but deriving from different namespaces.
From MSDN:
This is called a using alias directive

System.Xml.Linq could not be referenced properly at WEALTHLAB editor

I am trying to write C# code that does basically the following:
The code connects to MySql table and reads data from one MySql table (with r rows and c columns) - no issues here, everything when fine;
The code then defines and loads one DataTable with the help MySqlDataReader - again, no issues at this stage. Code is fine;
Each column of this datatable is in fact one strategy parameter of the strategy (i.e. each row of this datatable thus becomes one parameter set of the strategy.
Having said that:
the code then loops across all of the rows of this datatable;
the code gets the column values of each row one by one, and changes the XML elements one by one to replace old strategy values with the new ones - THIS IS WHERE I GET THE ERROR!!! I CANT MODIFY THE XML FILE FROM WITHIN WEALTHLAB C# EDITOR!!
that way, the strategy becomes configured with a new set of parameters each time;
the code then calls runDonor and runs the strategy with given (new) set of parameters - no problem at calling runDonor as well!;
and finally, certain statistics are recorded and inserted into one MySql table - there is no problem here as well;
**
Considering that a large chunk of this code is not related to wealthlab namespace, I coded most of the parts in Visual Studio first, compiled there using Visual Studio compiler, and seen that the code works just fine there! (i.e.the Xelement edits went just fine. I could see that the XML file is being modified after each loop when this code is run at Visual Studio!)
Then, I copied/pasted this code onto wealthscript editor, but unfortunately, the code did not compile!!.
**
The problem is at the region where I begin modifying the XML document, using ElementAt method.
More specifically, the ElementAt statement works fine at Visual Studio Editor, whereas it doesnt at Wealthlab editor.
To repeat, DESPITE THE FACT THAT I GIVE REFERENCE TO SAME DLLS AND USE THE SAME USING... STATEMENTS ON TOP OF THE CODE, Visual Studio returns no errors and runs thoroughly whereas Wealthlab returns this error:
'System.Collections.Generic.IEnumerable' <System.Xml.Linq> does not contain a definition for 'ElementAt'
and no extension method 'ElementAt' accepting a first argument of type
'System.Collections.Generic.IEnumerable' <System.Xml.Linq> could be found (are you
missing a using directive or an assembly reference?)"
I have been googling for ages, and could not come up with any decent solution yet.
And since I dont have a programming background, I really cant figure out why the same code, that uses the same references, and the same using directives, works at Visual Studio but yield errors at Wealthlab.
At this link, one solution alternative is vowed, but frankly, it is not english to me:
System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
**
So, my question would be two fold:
1- How can I overcome this interesting error that is related to proper refererencing to System.Xml.Linq? Obviously, this is where the problem lies.
2- Or, lets throw this out and start from the scratch: How do you guys modify your xmls? It would be splendid If I was provided a link of example codes that reads from xml files, or modifies them, saves them.
(Please, help...)
For you guys to repeat the same error I also attach here the whole of the code; copy this to your editors and see if the code runs ok:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Components;
using System.Collections;
using System.Xml;
using System.Xml.XPath;
using System.Linq;
using System.IO;
using System.Data;
using System.Xml.Linq; // THIS IS WHERE THE PROBLEM IS THIS REFERENCE COULD NOT BE SET PROPERLY!! WHY?!!
using MySql.Data.MySqlClient;
namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{
// public DateTime now;
public static XElement SourceXml;
public static XElement DonorStrategyXml;
public static string SourceXmlName;
public static string Ticker;
public static int nNames;
public string DonorStrategyXmlFolder;
public string DonorStrategyXmlName;
public string DonorStrategyXmlNameWithNoXml;
public string DonorStrategyXmlPathName;
protected override void Execute()
{
//....
//....
//....
DonorStrategyXmlFolder = #"C:\Users\Aykut\AppData\Roaming\Fidelity Investments\WealthLabDev\1.0.0.0\Data\Strategies\Customized\";
DonorStrategyXmlName = "sss.xml";
DonorStrategyXmlNameWithNoXml = "sss";
string DonorStrategyXmlPathName = DonorStrategyXmlFolder + DonorStrategyXmlName;
DonorStrategyXml = XElement.Load(DonorStrategyXmlPathName);
foreach (DataRow dbBandParameterRow in dbBandParameters.Rows) {
foreach (DataRow dbNNParameterRow in dbNNParameters.Rows) {
string ResultID = dbBandParameterRow["ResultID"].ToString();
int nE = 0;
//....
//....
//.THIS IS WHERE THE PROBLEM LIES AT!!...
DonorStrategyXml.Elements("ParameterValues").Descendants("double").ElementAt(nE).Value = dbBandParameterRow["RatioForUpper"].ToString();
nE++;
DonorStrategyXml.Elements("ParameterValues").Descendants("double").ElementAt(nE).Value = dbBandParameterRow["ADXPeriodForUpper"].ToString();
// output certain statistics of the strategy onto mysql // later!!!
//....
//....
PrintDebug("NetProfit:" + sp.Results.NetProfit.ToString());
}
}
}
} // class
} // nspace
The wealthlab support team returned with the following answer; and it worked.
Thanks.
...
the code won't compile in the current version 6.6 of Wealth-Lab's Editor.
To fix that, add references to System.Core.dll and System.Xml.Linq.dll from c:\windows\microsoft.net\framework64\v4.0.30319 (64-bit WLD, or c:\windows\microsoft.net\framework\v4.0.30319 for 32-bit WLD).
...

Why can't I use the following IEnumerable<string>?

I'm getting the following error:
Error 25 The type or namespace name 'IEnumerable' could not be found (are you missing a using directive or an assembly reference?) C:\Development\Leverage\Leverage\Reports\SurveyLevel.aspx.cs 39 17 Leverage
because of this line:
private IEnumerable<string> GetDateParameters()
How do I deal with this? I tried to add in the line:
using System.IDisposable
at the top, but this doesn't fix it.
As others have said, you're missing using System.Collections.Generic;.
But that's giving you a fish; we should be teaching you to catch your own fish.
The way to solve this problem on your own is:
Enter the name of the type into your favourite search engine, and see what comes back:
IEnumerable(T) Interface (System.Collections.Generic)
http://msdn.microsoft.com/en-us/library/9eekhta0
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
See the bit that I highlighted in bold there? That's the namespace that you're missing.
If you still get the error then you are likely missing a reference; you can find out which DLL you have failed to reference by clicking on the link and reading the documentation page; it will tell you which DLL to reference.
You are missing a using System.Collections.Generic; statement at the top of the code file.
The generic IEnumerable<T> type cannot be found directly.
You could declare the full name instead:
private System.Collections.Generic.IEnumerable<string> GetDateParameters()
IEnumerable is in System.Collections
IEnumerable<T> is in System.Collections.Generic
You just need to add System.Collections.Generic namespace top of your code.
IEnumerable<T> belongs on this namespace in mscorlib.dll assembly.
You can use it like;
private System.Collections.Generic.IEnumerable<string> GetDateParameters()
Above answers are good. In my case, even after following the above answers it did not resolve the issue. Still the red squiggly constantly appeared.
Issue was the Framework of the project. It was by default set to .NET Framework 4.0.3 and changing to .NET Framework 4.0.0 will also help.
Save your Project properties after the change, build and it should all work.
Hope this helps.

Roslyn - Can't find SyntaxNode.ReplaceNode()

Following this tutorial http://msdn.microsoft.com/en-us/vstudio/hh543922.aspx , I'm trying to use the ReplaceNode method that should be in the SyntaxNode class.
The thing is, I have this error: "Roslyn.Compiler.CSharp.SyntaxNode does not contain a definition for 'ReplaceNode'
Any ideas?
ReplaceNode is actually an extension method (so that it can return the type passed in). Make sure you have using Roslyn.Compilers; and using Roslyn.Compilers.CSharp;
Looks like an issue with that version of the CTP. Others are having the same issue, such as http://youtrack.jetbrains.com/issue/DOTP-4774 or http://youtrack.jetbrains.com/issue/DOTP-4836 .

Proxem's Antelope: interface not found "ISentenceSplitter"

I need to do some Natural Language Processing on various text inputs from user in a C# based desktop application. I am using Antelope for this purpose. The first step is to split the text into sentences. Following the documentation provided by Antelope, I used:
using Proxem.Antelope;
using Proxem.Antelope.Lexicon;
using Proxem.Antelope.Tools;
using Proxem.Antelope.LinkGrammar;
using Proxem.Antelope.Stanford;
using NUnit.Framework;
...
...
...
ISentenceSplitter splitter = new Tools.MIL_HtmlSentenceSplitter();
splitter.Text = text;
foreach (string sentence in splitter.Sentences)
{
// Process sentence…
}
Also, I have added references to these libraries as well. But it gives the error
The type or namespace name 'ISentenceSplitter' could not be found (are you missing a using directive or an assembly reference?) C:\Users\...
and
The type or namespace name 'Tools' could not be found (are you missing a using directive or an assembly reference?) C:\Users\...
I can't seem to figure out the solution. After a search on net I found out that other people are also having this problem but no one could actually found a solution. Can you please help me guys?
Simple answer is to avoid using this library. No offense to the authors, they might have done v.good and hard work but if it can not be utilized after all kinds of possible tries then it is useless. They mention in doc that a function belongs to a particular interface but when you go there, it doesn't exist in ANY of the available interfaces.
For those who are curious: I did contact the authors through their site but didn't get a reply even after 8 days.
There are other alternatives available like OpenNLP (java) or its C# counterpart SharpNLP.
I had the same problem. It stems from the fact that antelope dlls are compatible with
.net framework 2.0 and your application is set to use .net 4.0. (possibly)
I changed my application to .net 2.0 and trouble disappeared. :)
In addition I have used both SharpNLP and Antelope. Antelope has some superior features like
Collapse detection and port to wordnet which I had not seen in SharpNLP.
good luck.
Quite simple answer to this one: ISentenceSplitter is not in the actual release. I have v 0.8.7 which should be the same free 2009 version everyone else has To confirm this, I did a
grep -r ISentenceSplitter .
and nothing came back. Try to grep with another interface that does exist such as ILexicon and you see all the dlls that contain ILexicon.
Note that we are talking about a free version and Proxem, like other reasonable companies, want to market their technology as a profitable enterprise. Therefore you have to be happy with what you have, look into the paid solutions, use something else or write your own library which the community would be very happy to have.
I ran into that same error. Sali Hoo has the actual correct answer. This is not an answer for how to find or use ISentenceSplitter; however, it does show how Proxem's example application splits sentences as part of a larger process.
The example application that ships with the libraries can split sentences, so I figured the code allows for it. After reading a bit of code, I found that their example uses the Proxem.Antelope.Document class for sentence splitting and syntactical parsing. Rather than decompiling the libraries to see how the guts of the Document constructor uses ISentenceSplitter (I'm fairly confident that it does) I just used the class and it actually ended up performing all of the functions I needed, not just sentence splitting. I definitely recommend taking a look at how their example uses it, but here's the essence of my implementation:
public sealed class SyntaxService : ISyntaxService
{
public SyntaxService(IParser parser, ILexicon lexicon)
{
m_parser = parser;
m_lexicon = lexicon;
// These are the setting I needed, YMMV. They seem to add a lot of UI logic to this method :(
m_processingResources = new ProcessingResources(m_lexicon, null, null, m_parser, 5, null, false, null, null, null, null, false);
}
public IEnumerable<Sentence> GetSentences(string s)
{
IDocument document = new Document(s, m_processingResources);
return document.Select(Mappers.SentenceMapper.Map);
}
readonly IParser m_parser;
readonly ILexicon m_lexicon;
readonly IProcessingResources m_processingResources;
}
The Sentence class referenced here is a simplified syntax tree along with the original sentence value. If you're not super interested in this type of dependency injection friendly wrapping and mapping, you can just use the guts of GetSentences(string).

Categories