Select HTML node only when it has no class - c#

I'm using HTMLAgilityPack to grab a bunch of a tags. Some of the have 1 of several classes assigned and some have no class. It's those with no class that I need to grab.
I know that to grab a node by class we can do something like;
.SelectNodes("//table[#class=\"pagelinks\"]");
Similarly you can choose to ignore specific classes or id's with;
.SelectNodes("//table[not(#class=\"pagelinks\")]");
But is there a way to only grab a node when and only when it has no class?

The following code should select when there's no class attribute defined at all:
.SelectNodes("//table[not(#class)]");

This XPath will select tables that either have no class attribute, or that have a class attribute that is entirely whitespace (or blank):
//table[not(normalize-space(#class))]

Related

Serialize class properties with their types automatically

My boss have a strange request, he wants me to add a new function to serialize and deserialize all our products classes and add in the XML file all their property's types automatically.
I can't modify the classes to add new "types properties" before every "real property".
Is there a way to do this with [XmlAttributes] or something else ?
Thank you.
Maybe this is what you are looking for https://learn.microsoft.com/dotnet/api/system.xml.serialization.xmltypeattribute?view=netcore-3.1
this article describes the XmlTypeAttribute, which you can use to add a type attribute (in xml) on your properties tag.
[XmlType("aType")]
public string MyProperty {get;set;}

How to modify Solution/Project object?

I have defined attribute MyAttribute : Attribute that is supposed to be used exactly once within a class (only one constructor per class may have it and it can appear only once on ctor’s attribute list).
I have created a Roslyn analyzer to check this which marks every usage of such attribute (if used more than once) and allows user to pick fixture called "Leave this attribute occurrence and delete all others".
Now within FixProvider I need to return new modified Solution. It's not difficult to modify every Document that requires the fix (by using SyntaxRewriter to modify SyntaxTree inside). However, I have no idea how to modify Solution or Project - they don't have any method like "ReplaceProject"/"ReplaceDocument".
How to do that?
You could replace the text of a document using the following method:
solution = solution.WithDocumentText(currentDocument.Id,
currentDocumentSyntaxTree.GetText());

How do I use an EmailValidator in Spring.NET?

I'm trying to use validation in Spring.NET to validate that a string property of a class is a properly formatted e-mail address. I found the EmailValidator class (source, documentation), but I'm unsure of the syntax needed to actually use it with my class.
I'm trying this:
<v:email id="validate.myClass.MailFrom" test="MailFrom">
<v:message id="myClass.MailFrom" providers="myClassProvider"/>
</v:email>
but I get the error:
The element 'group' in namespace 'http://www.springframework.net/validation' has invalid child element 'email' in namespace 'http://www.springframework.net/validation'. List of possible elements expected: 'validator, required, condition, regex, ref, collection, group, any, exclusive' in namespace 'http://www.springframework.net/validation'.
The same namespace that contains EmailValidator also contains ConditionValidator, RegularExpressionValidator, etc., which seem to correspond to allowed child elements. That doesn't seem to be the case for EmailValidator. What am I missing?
Try something like
<v:validator test="MailFrom"
type="Spring.Validation.Validators.EmailValidator, Spring.Core">
<v:message id="myClass.MailFrom" providers="myClassProvider"/>
</v:validator>
the email validator is a custom validator, which means you have to specify its type. In xml configuration, you use the validator tag as for your own custom validators. See http://www.springframework.net/doc-latest/reference/html/validation.html#d4e3643. I get your point about the other validator classes in the namespace having their own XML element. Maybe the email validator is considered too specific to warrant its own XML element?

Add LINQ Auto-Generated Value Marker [Column(IsDbGenerated=true)] in Buddy Class

is it possible to decorate a field of a LINQ generated class with [Column(IsDbGenerated=true)] using a buddy class (which is linked to the LINQ class via [MetadataType(typeof(BuddyMetadata))]) ?
My goal is to be able to clear and repopulate the LINQ ORM designer without having to set the "Auto Generate Value" property manually every time to re-establish the fact that certain columns are autogenerated.
Thanks!
LINQ to SQL does not recognize buddy classes.
You can't just add the attribute to the partial either as the property is already defined in the other partial (this is what buddy classes were invented to solve).
One option would be to use my T4 template for VS that replicates the code-generation functionality of LINQ to SQL (I used to work on the product team) and you could put some logic in there to detect your auto-generated columns and emit the right attribute every time.
http://l2st4.codeplex.com
You can use a partial class - use your own partial class and decorate it with the attribute. It will be safe from the code generation.
If you need to decorate a method, you may be able to use partial methods as well.
As far as I know, fields cannot be decorated this way without the code generator destroying them on re-generation.

Extending Atom10FeedFormatter

I am creating an XML file which is based upon the Atom 1.0 specification. The .net Syndication classes are ideal for producing the Atom elements of my document but I need to extend this class so that I can create my own elements like below using strongly types C# classes. What is the best way to extend these classes so to get my desired results?
<myNS: userdata myNS:field=”first_name” >Fred Bloggs</myNS:userdata>
I have tried to create my own Feed and Item by inheriting from SyndicationFeed and SyndicationItem respectively but this means I will have to create my own feedFormatter class as the Atom10FeedFormatter class only takes SyndicationItems as parameters. I think I am going to have to create the following classes to get my desired result but wanted to put it to the community to see if its the correct way to go an if anybody else has done anything similar.
MyFeedFormatter : Atom10FeedFormatter
MyFeed : SyndicationFeed
MyItem : SyndicationItem
The .NET Syndication Feed supports extensions. It's possible through SyndicationFeed.ElementExtensions and SyndicationFeed.AttributeExtensions.
http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.elementextensions.aspx
I started off doing it this way but I need a way and even created some Extension Methods but I need to make the whole document creation process type safe.
Therefore I have created elements as objects (see below) which have public properties as there attributes. By just using the extensions I am leaving it open to errors if fields are incorrectly spelt etc and thus not having a valid XML doc.
myItem.userdata = new UserDataElement
{
Field = “first_name”,
Content = "Fred Bloggs"
};

Categories