String to HtmlDocument
The HtmlDocument
class is a wrapper around the native IHtmlDocument2
COM interface.
You cannot easily create it from a string.
You should use the HTML Agility Pack.
Getting HtmlDocument from string without using browser control
You can use HtmlAgilityPack .... For example:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var results = doc.DocumentNode
.Descendants("div")
.Select(n => n.InnerText);
Converting a string to an HTML document
The reason is because you declared ahtm as Nothing. instantiate it and see if it works.
Update:
HtmlDocument is a wrapper around an unmanaged class (IHtmlDocument). Try Declaring a WebBrower and then assigning the ahtm to the web browser document property.
WebBrowser wb = new WebBrowser();
HtmlDocument atm = wb.Document;
In other words, Web browser is the easiest way.
Update:
The alternative would be to use something like HtmlAgilityPack. http://htmlagilitypack.codeplex.com/
How to convert raw html (string) to htmlDocument in Java
Checking source code of HTMLDocument
gives you the answer.
It have a cool constructor to take html string.
public HTMLDocument(final String data) {
Charset cs = Charset.forName("utf-8");
this.data = data.getBytes(cs);
this.charset = cs;
}
So
HTMLDocument doc = new HTMLDocument(htmlStr);
C# Convert HtmlString into HTMLDocument
I think this StackOverflow discussion will help you.
You can find two ways to solve your problem:
- with HTML Agility Pack
- with the following code :
HTMLDocument doc = new HTMLDocument();
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
doc2.write(fileText);
Creating an HTMLDocument from a String of HTML (in Java)
Try to use HtmlEditorKit
class. It supports parsing of HTML content that can be read directly from String
(e.g. through StringReader
). There seems to be an article about how to do this.
Edit: To give an example, basically I think it could be done like this (aftrer the code is executed, htmlDoc
should contain the loaded document...):
Reader stringReader = new StringReader(string);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
parser.parse(stringReader, htmlDoc.getReader(0), true);
Convert Html String into HTMLDocument VBA
I am making use of method from excel-vba-http-request-download-data-from-yahoo-finance:
Sub Forex(currency1 As String, currency2 As String)
Dim url As String, html As String, id As String
Dim oResult As Variant, oLine As Variant, sRate As String
url = "http://finance.yahoo.com/q?s=" & currency1 & currency2 & "=X"
id = "<span id=""yfs_l10_" & currency1 & currency2 & "=x"">"
html = GetHTTPResult(url)
oResult = Split(html, vbLf)
For Each oLine In oResult
If InStr(1, oLine, id, vbTextCompare) Then
sRate = Split(Split(oLine, "<span id=""yfs_l10_audusd=x"">")(1), "</span>")(0)
Exit For
End If
Next
End Sub
Function GetHTTPResult(sURL As String) As String
Dim XMLHTTP As Variant, sResult As String
Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
XMLHTTP.Open "GET", sURL, False
XMLHTTP.send
sResult = XMLHTTP.responseText
Set XMLHTTP = Nothing
GetHTTPResult = sResult
End Function
get all html as a String from HTMLDocument
StringWriter writer = new StringWriter();
kit.write(writer, doc, 0, doc.getLength());
String s = writer.toString();
Related Topics
Are Java and C# Regular Expressions Compatible
How to Use HTML.Textboxfor with Input Type=Date
How to Prevent Xss (Cross Site Scripting) Whilst Allowing HTML Input
ASP.NET Core MVC - Client-Side Validation for Custom Attribute
How to Get Device Token in iOS 13 with Xamarin
How to Hook into Hardware Keyboard Events at an Application Level
How to Share an Enum Declaration Between C# and Unmanaged C++
Swift: Convert Nsdate to C# Ticks
How to Bind an Objective-C Static Library to Xamarin.iOS
Openssl.Net Porting a Ruby Example to C# (From Railscasts 143 Paypal-Security)
Entity Framework Initialization Is Slow -- What How to Do to Bootstrap It Faster
How to Consume a Blazor Component as a Web Component Within a Regular Non-Blazor HTML Page
Webapi Cannot Parse Multipart/Form-Data Post
How to Display a List of Images, from a Folder on Hard Drive, on ASP.NET Website