Closed rwecho closed 1 year ago
In the HtmlNode.Encapsulator.cs#L298, Is using OuterHtml better than InnerHtml?
The bug seems to be not adhering to the [XPath]
attribute's NodeReturnType value here (the NodeReturnType value is supposed to govern whether InnerText, InnerHtml or OuterHtml is used for producing the result for a property.
HtmlNode.Encapsulator.cs#L298:
isn't the only place exhibiting this issue, HtmlNode.Encapsulator.cs#L180 also forgets to take HasXPathAttribute.NodeReturnType into account: https://github.com/zzzprojects/html-agility-pack/blob/c12580b2025144d618d3397dec1fd81132e3f822/src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs#L180
After proper support for the HasXPathAttribute.NodeReturnType property has been implemented, then this should work:
[HasXPath]
class TestA
{
[XPath("a", NodeReturnType = ReturnType.OuterHtml)] // <--------- setting NodeReturnType here
public List<Item> Items { get; set; }
[HasXPath]
public class Item
{
[XPath(".", "href")]
[SkipNodeNotFound]
public string Href { get; set; }
[XPath(".")]
[SkipNodeNotFound]
public string Name { get; set; }
}
}
Side note: There is a related issue that also warrants fixing when the proper NodeReturnType support is being implemented. Currently, when a property of a simple type like string has a [XPath]
attribute with an invalid NodeReturnType value, a bare-bones and non-descript System.Exception is being thrown:
This should to be changed into a meaningful exception with a message that informs about the concrete cause of the exception.
@elgonzo I created a PR on your advice, please help me review it.
Hello @rwecho ,
We will close this issue has your PR has been merged and now available in the v1.11.50
Best Regards,
Jon
https://github.com/zzzprojects/html-agility-pack/blob/c12580b2025144d618d3397dec1fd81132e3f822/src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs#L298
In most situations, I like to use XPath attribute binding to a Property. But in the nested class, it can not access the parent node, the parent node's attribute often records some important information, like id.
In the HtmlNode.Encapsulator.cs#L298, Is using OuterHtml better than InnerHtml?