Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the code
C#
HtmlAgilityPack.HtmlDocument doc = (HtmlAgilityPack.HtmlDocument)webBrowser1.Document.DomDocument;
           string texts = doc.DocumentNode.SelectSingleNode("//div[@class='text-conent']/p]").InnerText;
           richTextBox1.Text = texts;

its error:
Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to class type 'HtmlAgilityPack.HtmlDocument'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

Don't have much experience with COM, can any one help me understand what this means and how i solve it ?
Posted
Comments
F-ES Sitecore 22-Jun-15 6:09am    
You're probably just not using the code right, DomDocument is not returning something of type HtmlAgilityPack.HtmlDocument. Go through the html agility pack tutorials and walkthroughs, I'm sure the proper code will be in there.

1 solution

You have to load the document as url or string. The cast won't work:


C#
var document = webBrowser1.Document;
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)document.DomDocument;

var htmlString= documentAsIHtmlDocument3.documentElement.innerHTML;

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlString);

//then you can use the doc:
string texts = doc.DocumentNode.SelectSingleNode("//div[@class='text-conent']/p]").InnerText;
richTextBox1.Text = texts;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900