HTML Linq with HtmlAgilityPack, or alternative, in PCL - c#

I have written a project on .NET 4 and am currently in the process of allowing it to run on Windows Phone as well. I am using HtmlAgilityPack, a well known library which allows Linq queries over HTML, and am only using the LoadHtml and Linq interfaces it provides.
Having converted the class libraries from .NET 4 to PCL (Portable Class Library) with support for .NET 4 and WP8, I cannot seem to use the HtmlAgilityPack library anymore. Is there a way to allow HtmlAgilityPack to function correctly under a PCL project or is there a variable alternative with a similar Linq interface that does work as intended?
EDIT: HtmlAgilityPack provides 9 different versions, none of which are compatible with PCL. None of them resolve dependencies from the references. For some versions, it may appear that it does but upon usage an error will be thrown with the usual 'cannot load, unresolved dependencies'.
EDIT #2 Since it easy to miss a small comment, I'll update this answer with the solution I came up with. I extracted what was needed for basic functionality and implemented the missing components to make everything work. The result is here https://github.com/Deathspike/HtmlAgilityPack-PCL

One option is to port the HTML Agility Pack source code to a PCL. You can run the PCL Compliance Analyzer over it to get an idea of how hard this will be.
Alternatively, use the abstraction pattern. Create a portable interface for the functionality you need (ie LoadHtml and Linq), and then implement that interface for each platform by calling into the HTML Agility Pack. Then your portable code can depend on the platform-specific implementation.
For more information, see this blog post: How to Make Portable Class Libraries Work for You

You've asked and answered your own question - haven't you?
The HtmlAgilityPack does not support use with Portable Class Libraries.
At best you'll need to look at porting/migrating the specific functionality you require in a way that will work on the platforms you are using.

Look at HtmlParserSharp, this is a C# port of the validator.nu HTML5 parser. The project should be very easy to build as a PCL library since it's more or less a straight C++ port and uses only the most basic of .NET framework classes, with a few updates to improve performance in C#.
While most the work I've done with HtmlParserSharp has been for CsQuery, which itself is a long way from being PCL compliant, there is no reason at all that HtmlParserSharp would't work perfectly well on it's own as a lean HTML parser for your purposes. The project includes an example of building a DOM based on an XmlElement, but the tree builder is an abstraction so you could easily change this to use your own tree node objects instead.

Related

How to deal with library dependency versions?

I want to write a library that leverages functionality from e.g. JSON .NET. Let's say I use version 8.0 of JSON .NET. When I distribute my library, anyone who uses it will have to use the same version of JSON .NET, and it will conflict with any versions they are already using.
I could isolate the serialization part of my library into a separate NuGet package, but that would still be dependent on the particular version.
How can I build my library in such a way that it provides such serialization capabilities out of the box, and yet does not restrict the client code in terms of third party packages? Are there any existing patterns for this kind of thing?
Actually, your assumption is not exactly correct. Other versions of dependencies can be used. In .NET, we use assembly unification to accomplish this. Essentially, the end user's config file can be written to say, "when the code needs version 8.0, it's okay to use 9.0". And most of the time, newer code is written to be reverse compatible.
Ultimately, you can't stress out about it. Comes with the territory.

How to call a c# dll from java program

I have a c# dll that needs to be called in Java.I see that there is a method using jni to call c++ dlls.How can I do it for a c# dll..Please help..I couldnt find any good material on this
From here:-
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET
Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
You can use Java Native Interface. Or you can create a COM assembly from the C# code and use J-Interop to invoke it.
If you have C# dll sources you need to use maybe the better way will be to translate it to Java using some tools like GrassHopper.
According to GrassHopper key feature explanation it can convert MSIL to Java bite code. So can use without sources of c# dll
Check this: http://www.javonet.com
If you look for quick and easy solution then Javonet should work fine for you. It is light counterpart of IKVM and J-Integra works also as native bridge.
All you have to do is:
add Javonet.jar do your project call
call Javonet.addReference("yourlib.dll")
use your .NET library like it was almost JAVA package
Sample:
NObject obj = Javonet.New("yourDotNetClass");
obj.invoke("YourMethod","arg1", 2);
The syntax is not strongly-typed and works like reflection but gives you fastest access to any custom .NET code, third-party libs or .NET framework as no changes are needed on .NET side. If you need it is also possible to implement custom strongly-typed wrappers.
I do recommend this bridge as in my opinion it is easiest to quickly get things done but also other native bridges are worth checking as this is best approach for such case.
I would avoid going into custom JNI or COM unless you have a lot of time and you just want to learn, if you need quick and reliable solution take one of third-party bridges.

Umbraco Code Library - Version Non-specific

I've created a code library for use with Umbraco, as you'd expect it does all of the common tasks that I use over and over. I work for a digital agency and we support sites that are built from Umbraco versions (4.5.x onwards).
To date we've always complied the library against the same dlls as we're using for the current project, but this isn't great and we've ended up with lots of different branches, one for each version. Having this many branches is a nightmare and I'm trying to find a solution that has one project that can be used to all versions.
I'm just wondering if anyone can think of or knows a way of having doing this or has any experience in this?
If you code purely to the INode interface then you should be able to create your library independent of the version. DynamicNode and DynamicMedia both implement INode.

How can I test the backward compatibility of API between .net assemblies

I have an assembly that provides an API and is used by some other assemblies. I need to verify that a newer version of API dll is still compatible with the older assemblies that were using the older version of API.
I've found a couple of questions that ask the same, but there are no answers that resolve my problem:
Tool to verify compatibility of a public APIs
Tool for backwards compatibility for the C#/.NET API
Suggested tools can only compare two assemblies and say if there are possible breaking changes in API, but not if the newest API really breaks the older assembly that uses it.
I'd like to find a tool or write a test that will be able to check whether each of the older dlls can work with my new API dll.
As for the changes in API more likely that I will only extend it, but even though it still can break the code in older assemblies. Some of the examples of such changes can be found here:
A definite guide to API-breaking changes in .NET
.NET: with respect to AssemblyVersion, what defines binary compatibility?
For now the only solution I see is to compile the source code of the older assemblies with the newest API, but I would like to do it only with assemblies and add them as part of my unit tests. Is there any better way I can handle that?
edit:
I'm looking for a tool that will be able to automate the process of verifying the backward compatibility between .net assemblies. (command line or with some api too)
What you want is to do a diff and generate a the list of breaking changes. Then you want to search if of your assemblies does use any of the broken APIs. You can do this with ApiChange tool to do the diff and to find any affected users of it.
To make it more concrete. If you have removed a method from an interface then you need to find all implementers and users of this method in classes which uses the interface method or any class that does implement this method.
ApiChange can search for implementers and users of specific methods on the command line with the commands -whoimplementsinterface and -whousesmethod. It is not automated at the command line but you can directly use the ApiChange.Api.dll to automate this queries.
Edit1:
I just forgot: The ApiChange tool has actually the functionality you are interested in already. It is the option
-ShowrebuildTargets -new -old [-old2 ] -searchin
We did use it in our department with good results. The only gotcha are the XML Intellisense files. If another target does not use the removed method but references it inside the XmlDoc the compiler will write a warning that a non existing method was referenced. This is quite hard to catch and would involve to parse the intellisense docu files as well. But this is quite an edge case.
I've spent the day looking around for an answer to this. It seems like the tools referenced on the related (unhelpfully closed) questions are now dead or as good as. But I've just taken a look at Telerik's assembly diff tool JustAssembly and this looks much better than rolling your own, which, if you look at their library seems to be a whole heap of work and likely to go wrong.
They have a UI which isn't of that much help from the point of view of integrating into your CI build, it is pretty basic, but you can build the library from source, which I've just done and the library looks like it has everything you need to get yourself up and running pretty quickly.

Approach to share code between Compact Framework, Silverlight, WP7 and full .NET runtime

I am creating libraries that I will use across Compact Framework, Silverlight, WP7 and the full .NET runtime. I am aware that the question around sharing between Compact Framework and full .NET, or between Silverlight and full .NET has been asked many times and I have been reading all the answers around that, however this situation is further complicated because I have to use VS2010 for Silverlight/WP7 and VS2008 for Compact Framework.
I therefore need to use multiple solutions along with multiple projects for this.
Is there a suggested "best-practice" approach for managing this, I am aware that I can create the multiple solutions/projects using add-file-as-link functionality to maintain the project, however this becomes a manual process open to error which I'd like to avoid.
Has anyone had any experience with automating the build of seperate frameworks, for example creating and maintaining a single .NET project, but having a custom build action which tweaks the solution and project files automatically and building several output assemblies for the different required frameworks. I am aware there is an added complications with making sure the correct references are generated.
Is there an existing framework that achieves this, I've had a search around but can't see anything. Alternatively is there an appetite for the creation of such a framework?
This session from PDC2010 might help:
http://blogs.microsoft.co.il/blogs/arik/archive/2010/10/31/pdc-2010-3-screen-coding-sharing-code-between-windows-phone-silverlight-and-net.aspx
Microsoft have announced that the Portable Library Project will be available H1 this year - so it should be available real soon - maybe at Mix?
Until then, the best advice seems to be to create a SL 3 class library project for most of your sharing - WP7, WPF, SL-Web, .Net desktop - but you'll then still need to do something special for WM6 (but for WM6 I still need to do lots of special things anyway - like it still insists on using VS2008!)

Categories

Resources