Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

System.Windows.Forms WebBrowser for Parsing HTML

Discussion in 'Scripting' started by MichaelARoberts, Feb 18, 2017.

  1. MichaelARoberts

    MichaelARoberts

    Joined:
    Aug 12, 2014
    Posts:
    7
    Hello, I've been trying to parse webpages via the WebBrowser class in System.Windows.Forms lib. I installed the System.Windows.Forms lib to Assets/Plugins to be able to use it, and after writing my code, and launching my game with an empty with my code/script attached, the game crashes/goes unresponsive. Not sure if I have an unintentional loop somewhere or what. Would appreciate feedback.

    Here is the code I'm using:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Text.RegularExpressions;
    3. using System.Collections.Generic;
    4. using System.Windows.Forms;
    5. using UnityEngine;
    6.  
    7. public class Parser : MonoBehaviour {
    8.  
    9.     WebBrowser request = new WebBrowser();
    10.  
    11.     void Start()
    12.     {
    13.         request.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(docCompleted);
    14.         request.Navigate("https://www.google.com/");
    15.     }
    16.  
    17.     void docCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    18.     {
    19.         HtmlDocument doc = request.Document;
    20.         HtmlElementCollection tagCollection;
    21.         tagCollection = doc.GetElementsByTagName("a");
    22.         Debug.Log(tagCollection);
    23.     }
    24. }
    25.  
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Looking at the .Net 2.0 documentation your code seems entirely correct. Are you positive you got the 2.0 .Net lib when you installed it?
     
  3. MichaelARoberts

    MichaelARoberts

    Joined:
    Aug 12, 2014
    Posts:
    7
    Almost positive. I copied the lib from the monospace folder, so it should be working as intended. In addition I changed the script type from .NET 2.0 subset to .NET 2.0