Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HtmlNode.GetAttributeValue(string name, bool def) not working in version 1.11.32 #437

Closed
holatom opened this issue Apr 22, 2021 · 3 comments
Assignees

Comments

@holatom
Copy link

holatom commented Apr 22, 2021

Calling HtmlNode.GetAttributeValue(string name, bool def) method always returns default value in version 1.11.32 of Html Agility Pack.

The problem is with this code in GetAttributeValue(string name, T def) whitch is called with bool as the T type parameter.

        TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
        try
        {
            if (converter != null && converter.CanConvertTo(htmlAttribute.Value.GetType()))
            {
                return (T)converter.ConvertTo(htmlAttribute.Value, typeof(T));
            }

            return (T)(object)htmlAttribute.Value;
        }
        catch
        {
            return def;
        }

htmlAttribute.Value is for example "true" of type string.
converter is BooleanConverter and calling converter.ConvertTo throws exception:

System.NotSupportedException: 'BooleanConverter' is unable to convert 'System.String' to 'System.Boolean'.

  • System.ComponentModel.TypeConverter.GetConvertToException(object, System.Type)
  • System.ComponentModel.TypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext, System.Globalization.CultureInfo, object, System.Type)
  • System.ComponentModel.TypeConverter.ConvertTo(object, System.Type)

exception is catched by catch block and default value is returned.

@JonathanMagnan JonathanMagnan self-assigned this Apr 22, 2021
@JonathanMagnan
Copy link
Member

Hello @holatom ,

Thank you for reporting,

I improved the code by using the converter code I'm using in another of my project: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/src/Z.Core/System.Object/Convert/Object.To.cs

Everything seems to look fine with this scenario.

Could you try it and let me know that we fixed the issue correctly?

Best Regards,

Jon

@holatom
Copy link
Author

holatom commented Apr 24, 2021

Yes, issue is fixed.
Thanks.

@JonathanMagnan
Copy link
Member

Awesome @holatom !

We are glad to hear that the issue is fixed!

Don't hesitate to contact us with any questions, issues, or feedback.

Best regards,

Jon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants