|
11 | 11 | using System.Collections;
|
12 | 12 | using System.Collections.Generic;
|
13 | 13 | using System.Reflection;
|
14 |
| -using System.Xml; |
15 | 14 | using System.Xml.XPath;
|
16 | 15 |
|
17 | 16 | namespace HtmlAgilityPack
|
@@ -201,6 +200,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
|
201 | 200 | }
|
202 | 201 | else // It target attribute of HTMLTag
|
203 | 202 | {
|
| 203 | + ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(xPathAttribute); |
204 | 204 | result = htmlNode.GetAttributeValue(xPathAttribute.AttributeName, null);
|
205 | 205 | }
|
206 | 206 |
|
@@ -329,6 +329,8 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
|
329 | 329 | }
|
330 | 330 | else // It target attribute
|
331 | 331 | {
|
| 332 | + ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(xPathAttribute); |
| 333 | + |
332 | 334 | foreach (HtmlNode node in nodeCollection)
|
333 | 335 | {
|
334 | 336 | string nodeAttributeValue = node.GetAttributeValue(xPathAttribute.AttributeName, null);
|
@@ -386,6 +388,15 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
|
386 | 388 | }
|
387 | 389 | #endregion targetObject_NOTDefined_XPath
|
388 | 390 | }
|
| 391 | + |
| 392 | + |
| 393 | + private static void ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(XPathAttribute xPathAttr) |
| 394 | + { |
| 395 | + if (xPathAttr.IsNodeReturnTypeExplicitlySet && !string.IsNullOrEmpty(xPathAttr.AttributeName)) |
| 396 | + { |
| 397 | + throw new InvalidNodeReturnTypeException("Specifying a ReturnType value not allowed for XPathAttribute annotations targeting element attributes"); |
| 398 | + } |
| 399 | + } |
389 | 400 | }
|
390 | 401 |
|
391 | 402 |
|
@@ -749,7 +760,7 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
|
749 | 760 | case ReturnType.OuterHtml:
|
750 | 761 | return node.OuterHtml;
|
751 | 762 | default:
|
752 |
| - throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString()); |
| 763 | + throw new InvalidNodeReturnTypeException(string.Format("Invalid ReturnType value {0}", returnType)); |
753 | 764 | };
|
754 | 765 | }
|
755 | 766 | }
|
@@ -852,20 +863,6 @@ public XPathAttribute(string xpathString, string attributeName)
|
852 | 863 | AttributeName = attributeName;
|
853 | 864 | _nodeReturnType = ReturnType.InnerText;
|
854 | 865 | }
|
855 |
| - |
856 |
| - |
857 |
| - /// <summary> |
858 |
| - /// Specify Xpath and Attribute to find related Html Node and its attribute value. |
859 |
| - /// </summary> |
860 |
| - /// <param name="xpathString"></param> |
861 |
| - /// <param name="attributeName"></param> |
862 |
| - /// <param name="nodeReturnType">Specify you want the output include html text too.</param> |
863 |
| - public XPathAttribute(string xpathString, string attributeName, ReturnType nodeReturnType) |
864 |
| - { |
865 |
| - XPath = xpathString; |
866 |
| - AttributeName = attributeName; |
867 |
| - NodeReturnType = nodeReturnType; |
868 |
| - } |
869 | 866 | }
|
870 | 867 |
|
871 | 868 |
|
@@ -955,6 +952,26 @@ public MissingXPathException(string message) : base(message) { }
|
955 | 952 | public MissingXPathException(string message, Exception inner) : base(message, inner) { }
|
956 | 953 | }
|
957 | 954 |
|
| 955 | + |
| 956 | + /// <summary> |
| 957 | + /// Exception that occurs when an XPathAttribute annotation has an invalid ReturnType specified. |
| 958 | + /// </summary> |
| 959 | + public class InvalidNodeReturnTypeException : Exception |
| 960 | + { |
| 961 | + /// <summary> |
| 962 | + /// |
| 963 | + /// </summary> |
| 964 | + /// <param name="message"></param> |
| 965 | + public InvalidNodeReturnTypeException(string message) |
| 966 | + : base(message) { } |
| 967 | + |
| 968 | + /// <summary> |
| 969 | + /// |
| 970 | + /// </summary> |
| 971 | + /// <param name="message"></param> |
| 972 | + /// <param name="inner"></param> |
| 973 | + public InvalidNodeReturnTypeException(string message, Exception inner) : base(message, inner) { } |
| 974 | + } |
958 | 975 | }
|
959 | 976 |
|
960 | 977 | #if FX20
|
|
0 commit comments