MSHTML...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mat

    #1

    MSHTML...

    Hi all.
    I need to build a little HTML parser. I made 2 versions, for console e for
    window.
    In console mode it seems to work (but not always, sometimes it doesn't parse
    the HTML file), in window mode (target winexe) I get no result...
    This is my code:

    === CODE ===
    using MSHTML;
    using System;
    using System.Drawing;
    using System.Windows. Forms;

    public class HTMLParser : Form
    {
    [STAThread] public static int Main( string[] args )
    {
    Application.Run ( new HTMLParser() );
    return 0;
    }

    public HTMLParser()
    {
    this.Text = "HTML Parser";
    this.StartPosit ion = FormStartPositi on.Manual;
    this.Location = new Point( 100, 100 );
    this.Size = new Size( 200, 200 );

    Button button = new Button();
    button.Text = "Parse";
    button.Location = new Point( 10, 80 );
    button.Size = new Size( 80, 25 );

    button.Click += new System.EventHan dler( ButtonClick );
    this.Controls.A dd( button );
    }

    private void ButtonClick( object sender, EventArgs evArgs )
    {
    try
    {
    HTMLDocument document = new HTMLDocument();
    IHTMLDocument2 iDocument2;

    ( (IHTMLDocument2 )document ).write( "<html></html>" );
    ( (IHTMLDocument2 )document ).close();

    iDocument2 = ( (IHTMLDocument4 )document ).createDocumen tFromUrl(
    "file:///" + System.Environm ent.CurrentDire ctory + "\\myDoc.ht ml", "null" );

    IHTMLElementCol lection elemColl = (IHTMLElementCo llection)
    iDocument2.all;
    foreach( IHTMLElement elem in elemColl )
    MessageBox.Show ( elem.tagName );
    }
    catch( Exception e )
    {
    MessageBox.Show ( e.ToString() );
    }
    }
    }
    ======

    Someone can hel me?
    I tried also to use UCOMIPersistFil e and Load function... same results.

    Byez, -Mat-


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: MSHTML...

    Mat,

    Does it work when you debug it, and not work when you don't debug it? I
    have a feeling that because you are not waiting for the parsing to complete
    it might be bombing. Try placing a try/catch block around your code in the
    event handler for the button click, and see if an exception is thrown.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Mat" <indirizzo@priv ato.it> wrote in message
    news:S5Y6b.4256 0$R32.1356344@n ews2.tin.it...[color=blue]
    > Hi all.
    > I need to build a little HTML parser. I made 2 versions, for console e for
    > window.
    > In console mode it seems to work (but not always, sometimes it doesn't[/color]
    parse[color=blue]
    > the HTML file), in window mode (target winexe) I get no result...
    > This is my code:
    >
    > === CODE ===
    > using MSHTML;
    > using System;
    > using System.Drawing;
    > using System.Windows. Forms;
    >
    > public class HTMLParser : Form
    > {
    > [STAThread] public static int Main( string[] args )
    > {
    > Application.Run ( new HTMLParser() );
    > return 0;
    > }
    >
    > public HTMLParser()
    > {
    > this.Text = "HTML Parser";
    > this.StartPosit ion = FormStartPositi on.Manual;
    > this.Location = new Point( 100, 100 );
    > this.Size = new Size( 200, 200 );
    >
    > Button button = new Button();
    > button.Text = "Parse";
    > button.Location = new Point( 10, 80 );
    > button.Size = new Size( 80, 25 );
    >
    > button.Click += new System.EventHan dler( ButtonClick );
    > this.Controls.A dd( button );
    > }
    >
    > private void ButtonClick( object sender, EventArgs evArgs )
    > {
    > try
    > {
    > HTMLDocument document = new HTMLDocument();
    > IHTMLDocument2 iDocument2;
    >
    > ( (IHTMLDocument2 )document ).write( "<html></html>" );
    > ( (IHTMLDocument2 )document ).close();
    >
    > iDocument2 = ( (IHTMLDocument4 )document ).createDocumen tFromUrl(
    > "file:///" + System.Environm ent.CurrentDire ctory + "\\myDoc.ht ml",[/color]
    "null" );[color=blue]
    >
    > IHTMLElementCol lection elemColl = (IHTMLElementCo llection)
    > iDocument2.all;
    > foreach( IHTMLElement elem in elemColl )
    > MessageBox.Show ( elem.tagName );
    > }
    > catch( Exception e )
    > {
    > MessageBox.Show ( e.ToString() );
    > }
    > }
    > }
    > ======
    >
    > Someone can hel me?
    > I tried also to use UCOMIPersistFil e and Load function... same results.
    >
    > Byez, -Mat-
    >
    >[/color]


    Comment

    • Mat

      #3
      Re: MSHTML...

      > Does it work when you debug it, and not work when you don't debug it?
      I[color=blue]
      > have a feeling that because you are not waiting for the parsing to[/color]
      complete[color=blue]
      > it might be bombing. Try placing a try/catch block around your code in[/color]
      the[color=blue]
      > event handler for the button click, and see if an exception is thrown.[/color]

      I don't use Visual Studio .NET so I can't use the Visual Debugger.
      I don't know how does the cordbg work...

      Uhm... the code of my event handler is inside a try/catch block
      Another thing, if an exception is thrown the program execution is usually
      interrupted...


      Comment

      Working...