Example #1
0
        /// <summary>
        /// Returns the HtmlAttribute.Value property of the HtmlAttribute instance as a string.
        /// </summary>
        /// <param name="htmlAttributeInstance">The instance of HtmlAttribute to get the property from</param>
        /// <returns>The HtmlAttribute.Value property of the HtmlAttribute instance as a string</returns>
        public static string HtmlAttribute_GetProperty_Value(object htmlAttributeInstance)
        {
            if (!HtmlAgilityPackWrapper._HtmlAttributeType.IsAssignableFrom(htmlAttributeInstance.GetType()))
            {
                throw new InvalidOperationException(HtmlAgilityPackWrapper._HtmlAttributeType + " is not assignable from htmlAttributeInstance (" + htmlAttributeInstance.GetType() + ")");
            }

            HtmlAgilityPackWrapper.InitializeIfNeeded();
            return((string)HtmlAgilityPackWrapper._HtmlAttribute_Property_Value.GetValue(htmlAttributeInstance, null));
        }
Example #2
0
        /// <summary>
        /// Returns the HtmlNode.ChildNodes property of the HtmlNode instance as an IEnumerable.
        /// Although the items in the returned collection are type object, they are actually HtmlNode.
        /// </summary>
        /// <param name="htmlNodeInstance">The instance of HtmlNode to get the property from</param>
        /// <returns>The HtmlNode.ChildNodes property of the HtmlNode instance as an IEnumerable</returns>
        public static IEnumerable <object> HtmlNode_GetProperty_ChildNodes(object htmlNodeInstance)
        {
            if (!HtmlAgilityPackWrapper._HtmlNodeType.IsAssignableFrom(htmlNodeInstance.GetType()))
            {
                throw new InvalidOperationException(HtmlAgilityPackWrapper._HtmlNodeType + " is not assignable from htmlNodeInstance (" + htmlNodeInstance.GetType() + ")");
            }

            HtmlAgilityPackWrapper.InitializeIfNeeded();
            return(((System.Collections.IEnumerable)HtmlAgilityPackWrapper._HtmlNode_Property_ChildNodes.GetValue(htmlNodeInstance, null)).Cast <object>());
        }
Example #3
0
        /// <summary>
        /// Returns the result of calling the HtmlNode.SelectSingleNode(xpath) method on the HtmlNode instance.
        /// Although the return type is object, it is actually an HtmlNode.
        /// </summary>
        /// <param name="htmlNodeInstance">The instance of HtmlNode to invoke SelectNodes(xpath) on</param>
        /// <param name="xpath">The XPath expression.</param>
        /// <returns>The result of calling the HtmlNode.SelectSingleNode(xpath) method on the HtmlNode instance</returns>
        public static object HtmlNode_InvokeMethod_SelectSingleNode(object htmlNodeInstance, string xpath)
        {
            if (!HtmlAgilityPackWrapper._HtmlNodeType.IsAssignableFrom(htmlNodeInstance.GetType()))
            {
                throw new InvalidOperationException(HtmlAgilityPackWrapper._HtmlNodeType + " is not assignable from htmlNodeInstance (" + htmlNodeInstance.GetType() + ")");
            }

            HtmlAgilityPackWrapper.InitializeIfNeeded();
            return(HtmlAgilityPackWrapper._HtmlNode_Method_SelectSingleNode.Invoke(htmlNodeInstance, new object[] { xpath }));
        }
Example #4
0
        /// <summary>
        /// Returns the result of calling the HtmlNode.SelectNodes(xpath) method on the HtmlNode instance as an IEnumerable.
        /// Although the items in the returned collection are type object, they are actually HtmlNode.
        /// </summary>
        /// <param name="htmlNodeInstance">The instance of HtmlNode to invoke SelectNodes(xpath) on</param>
        /// <param name="xpath">The XPath expression.</param>
        /// <returns>The result of calling the HtmlNode.SelectNodes(xpath) method on the HtmlNode instance as an IEnumerable</returns>
        public static IEnumerable <object> HtmlNode_InvokeMethod_SelectNodes(object htmlNodeInstance, string xpath)
        {
            if (!HtmlAgilityPackWrapper._HtmlNodeType.IsAssignableFrom(htmlNodeInstance.GetType()))
            {
                throw new InvalidOperationException(HtmlAgilityPackWrapper._HtmlNodeType + " is not assignable from htmlNodeInstance (" + htmlNodeInstance.GetType() + ")");
            }

            HtmlAgilityPackWrapper.InitializeIfNeeded();
            return(((System.Collections.IEnumerable)HtmlAgilityPackWrapper._HtmlNode_Method_SelectNodes.Invoke(htmlNodeInstance, new object[] { xpath })).Cast <object>());
        }
Example #5
0
        /// <summary>
        /// Returns the HtmlDocument.DocumentNode property of the HtmlDocument instance.
        /// Although the return type is object, it is actually an HtmlNode.
        /// </summary>
        /// <param name="htmlDocumentInstance">The instance of HtmlDocument to get the property from</param>
        /// <returns>The HtmlDocument.DocumentNode property of the HtmlDocument instance</returns>
        public static object HtmlDocument_GetProperty_DocumentNode(object htmlDocumentInstance)
        {
            if (!HtmlAgilityPackWrapper._HtmlDocumentType.IsAssignableFrom(htmlDocumentInstance.GetType()))
            {
                throw new InvalidOperationException(HtmlAgilityPackWrapper._HtmlDocumentType + " is not assignable from htmlDocumentInstance (" + htmlDocumentInstance.GetType() + ")");
            }

            HtmlAgilityPackWrapper.InitializeIfNeeded();
            return(HtmlAgilityPackWrapper._HtmlDocument_Property_DocumentNode.GetValue(htmlDocumentInstance, null));
        }
Example #6
0
        /// <summary>
        /// Returns the result of calling the HtmlWeb.Load(url) method on the HtmlWeb instance.
        /// Although the return type is object, it is actually an HtmlDocument.
        /// </summary>
        /// <param name="htmlWebInstance">The instance of HtmlWeb to invoke Load(url) on</param>
        /// <param name="url">The requested URL</param>
        /// <returns>The result of calling the HtmlWeb.Load(url) method on the HtmlWeb instance</returns>
        public static object HtmlWeb_InvokeMethod_Load(object htmlWebInstance, string url)
        {
            if (!HtmlAgilityPackWrapper._HtmlWebType.IsAssignableFrom(htmlWebInstance.GetType()))
            {
                throw new InvalidOperationException(HtmlAgilityPackWrapper._HtmlWebType + " is not assignable from htmlWebInstance (" + htmlWebInstance.GetType() + ")");
            }

            HtmlAgilityPackWrapper.InitializeIfNeeded();
            return(HtmlAgilityPackWrapper._HtmlWeb_Method_Load.Invoke(htmlWebInstance, new object[] { url }));
        }
Example #7
0
        /// <summary>
        /// Returns the HtmlNode.InnerText property of the HtmlNode instance as a string.
        /// Any HTML characters are already decoded to ascii.
        /// </summary>
        /// <param name="htmlNodeInstance">The instance of HtmlNode to get the property from</param>
        /// <returns>The HtmlNode.InnerText property of the HtmlNode instance as a string</returns>
        public static string HtmlNode_GetProperty_InnerText(object htmlNodeInstance)
        {
            if (!HtmlAgilityPackWrapper._HtmlNodeType.IsAssignableFrom(htmlNodeInstance.GetType()))
            {
                throw new InvalidOperationException(HtmlAgilityPackWrapper._HtmlNodeType + " is not assignable from htmlNodeInstance (" + htmlNodeInstance.GetType() + ")");
            }

            HtmlAgilityPackWrapper.InitializeIfNeeded();

            string innerTextString = (string)HtmlAgilityPackWrapper._HtmlNode_Property_InnerText.GetValue(htmlNodeInstance, null);

            // Before returning, convert HTML characters to ASCII.  For example, &#39; ==> '
            return(System.Net.WebUtility.HtmlDecode(innerTextString));
        }
Example #8
0
        /// <summary>
        /// Initializes everything the wrapper needs if it hasn't already been initialized.
        /// </summary>
        /// <remarks>Extracts the DLL to disk if it doesn't exist, then loads the
        /// assembly and plucks types, methods and properties that are needed.</remarks>
        private static void InitializeIfNeeded()
        {
            if (!HtmlAgilityPackWrapper._IsInitialized)
            {
                string exeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                string htmlAgilityPackDLLFilename  = @"HtmlAgilityPack.dll";
                string htmlAgilityPackFullPathName = System.IO.Path.Combine(exeDir, htmlAgilityPackDLLFilename);

                // Extract the DLL if missing or outdated
                if (!System.IO.File.Exists(htmlAgilityPackFullPathName))
                {
                    System.IO.File.WriteAllBytes(htmlAgilityPackFullPathName, OCTGNDeckConverter.Properties.Resources.HtmlAgilityPack);
                }
                else
                {
                    System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(htmlAgilityPackFullPathName);
                    Version ver = assembly.GetName().Version;

                    // Check the version of HtmlAgilityPack
                    if (ver.Major != 1 || ver.Minor != 4 || ver.Build != 6)
                    {
                        System.IO.File.WriteAllBytes(htmlAgilityPackFullPathName, OCTGNDeckConverter.Properties.Resources.HtmlAgilityPack);
                    }
                }

                if (System.IO.File.Exists(htmlAgilityPackFullPathName))
                {
                    HtmlAgilityPackWrapper._HTMLAgilityPackAssembly = System.Reflection.Assembly.LoadFile(htmlAgilityPackFullPathName);
                    HtmlAgilityPackWrapper._HTMLAgilityPackTypes    = HtmlAgilityPackWrapper._HTMLAgilityPackAssembly.GetTypes();

                    HtmlAgilityPackWrapper.InitializeHtmlWeb();
                    HtmlAgilityPackWrapper.InitializeHtmlDocument();
                    HtmlAgilityPackWrapper.InitializeHtmlNode();
                    HtmlAgilityPackWrapper.InitializeHtmlAttribute();

                    HtmlAgilityPackWrapper._IsInitialized = true;
                }
                else
                {
                    throw new InvalidOperationException("HTMLAgilityPack.dll cannot be found.  Ensure it is in the same directory, or reinstall the plugin.");
                }
            }
        }
Example #9
0
 /// <summary>
 /// Returns a new instance of HtmlWeb.  Although the return type is object, it is actually an HtmlWeb.
 /// </summary>
 /// <returns>A new instance of HtmlWeb</returns>
 public static object HtmlWeb_CreateInstance()
 {
     HtmlAgilityPackWrapper.InitializeIfNeeded();
     return(Activator.CreateInstance(HtmlAgilityPackWrapper._HtmlWebType, new object[] { }));
 }