Is There a Jquery-Like CSS/HTML Selector That Can Be Used in C#

Is there a jQuery-like CSS/HTML selector that can be used in C#?

You should definitely see @jamietre's CsQuery. Check out his answer to this question!

Fizzler and Sharp-Query provide similar functionality, but the projects seem to be abandoned.

HTML Selector Library

You should check out the Html Agility pack, available here.
Here's a use case from their website, that uses XPATH selectors:

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att);
}
doc.Save("file.htm");

JQuery .closest() equivalent in HtmlAgilityPack?

As there is no built-in method currently, you can write a Extension method to achieve this.

I have written a simple extension method which can be used to find elements with tagName, ID and class names that you can use.

Anyways it can be further extended easily to match other selectors.

public static class HtmlAgilityPackExtensions
{
public static HtmlNode Closest(this HtmlNode node, string jQuerySelector)
{
if (node == null) return null;
string tagName = "", id = "";
var classes = new List<string>();

if (jQuerySelector.Contains("."))
{
var parts = jQuerySelector.Split('.');

if (!string.IsNullOrWhiteSpace(parts[0]))
{
tagName = parts[0];
}

for (int i = 1; i < parts.Length; i++)
{
classes.Add(parts[i]);
}
}

if (jQuerySelector.Contains("#"))
{
var parts = jQuerySelector.Split('#');

if (!string.IsNullOrWhiteSpace(parts[0]))
{
tagName = parts[0];
}

id = parts[1];
}

if (string.IsNullOrWhiteSpace(tagName) && string.IsNullOrWhiteSpace(id) && classes.Count == 0)
{
tagName = jQuerySelector;
}

HtmlNode closestParent = null;

while (node.ParentNode != null && closestParent == null)
{
var isClosest = true;
node = node.ParentNode;

if (!string.IsNullOrWhiteSpace(tagName))
{
isClosest = node.Name == tagName;
}

if (isClosest && !string.IsNullOrWhiteSpace(id))
{
isClosest = node.Id == id;
}

if (isClosest && classes.Count > 0)
{
var classNames = node.GetAttributeValue("class", "");
if (!string.IsNullOrWhiteSpace(classNames))
{
foreach (string c in classes)
{
isClosest = classNames.Contains(c);
if (!isClosest) break;
}
}
}

if (isClosest)
{
closestParent = node;
}
}

return closestParent;
}
}

Test Code

       var html = "<div><div id='parent1' class='parent'><span id='parent2' class='parent'><div id='parent3' class='parent'><div id='TestNode' class='child'>Test node</div></div></span></div></div>";
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var testNode1 = htmlDoc.DocumentNode.SelectSingleNode("//div[@id='TestNode']");
if (testNode1 != null)
{
var parent1 = testNode1.Closest(".parent");
var parent2 = testNode1.Closest("#parent1");
var parent3 = testNode1.Closest("span.parent");
var nonExistingParent = testNode1.Closest("span.parent1");
}

Is there a way to use JQuery style selectors against XML/HTML in C#?

Okay, I found my own answer:

CsQuery which is open source on GitHub allows just what I needed.

Is embedding CSS/jQuery code in C# code bad?

Off the top of my head I can think of a couple of issues, non fatal however.

In no particular order:

  1. You lose the ability to cache your JavaScript files, on either the server or on the client.
  2. You increase the side of your page. If every button has a lot of embedded JavaScript, then the page size, thus load times, are increased.
  3. Debugging will become extremely difficult.
  4. Unobtrusive JavaScript is your friend!
  5. Maintenance becomes more complex as you need to remember where in the C# code that the hard-coded JavaScript strings are.
  6. Intellisense is lost. Visual studio has a fantastic JavaScript editor, you lose all of that functionality by hard-coding the strings
  7. Did I mention Unobtrusive JavaScript is your friend!
  8. You lose the benefits of separation of functionality.
  9. If you have duplicate buttons with the same functionality, then you have duplicate code.

I'm sure there is a bunch I have missed.

Edit HTML files manipulating DOM in a jQuery style

I am not sure of the right way but it sounds like you are familiar with C# and would think writing a class library would be the least overhead for automation. Here are some potential solutions:

  1. Scripting Library (e.g., C#.NET) - You can use a library like the one you mentioned or something like ScriptSharp if you want to use DOM manipulation. If the HTML has appropriate closing tags you can also use LINQ to easily navigate the HTML (or something like the HTML Agility Pack found on CodePlex). I would even recommend using Mustache with an HTML file template in C#.

  2. JavaScript Library - If you wanted to stay in pure JavaScript you can use Node.js. There are file manipulation libraries you can use.

  3. Headless Browsers - Haven't thought through being able to save the resulting HTML automatically but you can use something like jsTestDriver or Phantom.js

You can go with the plugins in editors as well, but I would stick with a Java, C#, python, etc. library that you can potentially call from existing application or schedule as a job/service.



Related Topics



Leave a reply



Submit