本文整理汇总了C#中HtmlDocument.LoadHtml方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument.LoadHtml方法的具体用法?C# HtmlDocument.LoadHtml怎么用?C# HtmlDocument.LoadHtml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HtmlDocument
的用法示例。
在下文中一共展示了HtmlDocument.LoadHtml方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCallingExistingMember
public void TestCallingExistingMember()
{
var doc = new HtmlDocument();
doc.LoadHtml("<html><body class=\"asdfasd\"><p>asdf asdf sdf</p></body></html>");
dynamic docElement = doc.DocumentNode;
var item = docElement.Closed;
Assert.IsInstanceOf<bool>(item);
}
示例2: TestCallingExistingFunction
public void TestCallingExistingFunction()
{
var doc = new HtmlDocument();
doc.LoadHtml("<html><body class=\"asdfasd\"><p>asdf asdf sdf</p></body></html>");
dynamic docElement = doc.DocumentNode;
var item = docElement.Descendants();
Assert.IsInstanceOf<IEnumerable<HtmlNode>>(item);
}
示例3: TestGetMember
public void TestGetMember()
{
var doc = new HtmlDocument();
doc.LoadHtml("<html><body><p>asdf asdf sdf</p></body></html>");
dynamic docElement = doc.DocumentNode;
var item = docElement.Html.Body;
Assert.IsNotNull(item);
Assert.IsInstanceOf<HtmlNode>(item);
}
示例4: TestGetAttribute
public void TestGetAttribute()
{
var doc = new HtmlDocument();
doc.LoadHtml("<html><body class=\"asdfasd\"><p>asdf asdf sdf</p></body></html>");
dynamic docElement = doc.DocumentNode;
var item = docElement.Html.Body._Class;
Assert.IsNotNull(item);
Assert.IsInstanceOf<HtmlAttribute>(item);
}
示例5: ConvertHtml
public string ConvertHtml(string html)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
StringWriter sw = new StringWriter();
ConvertTo(doc.DocumentNode, sw);
sw.Flush();
return sw.ToString();
}
示例6: EnsureAttributeOriginalCaseIsPreserved
public void EnsureAttributeOriginalCaseIsPreserved()
{
var html = "<html><body><div AttributeIsThis=\"val\"></div></body></html>";
var doc = new HtmlDocument
{
OptionOutputOriginalCase = true
};
doc.LoadHtml(html);
var div = doc.DocumentNode.Descendants("div").FirstOrDefault();
var writer = new StringWriter();
div.WriteAttributes(writer, false);
var result = writer.GetStringBuilder().ToString();
Assert.AreEqual(" AttributeIsThis=\"val\"", result);
}
示例7: LoadHtmlAsXml
/// <summary>
/// Returns an XML document from a given URL.
/// </summary>
/// <param name="web">The web.</param>
/// <param name="url">The URL.</param>
/// <param name="format">The format.</param>
/// <param name="absolutizeLinks">if set to <c>true</c> [absolutize links].</param>
/// <returns></returns>
public static XmlDocument LoadHtmlAsXml(HtmlWeb web, string url, string format,
bool absolutizeLinks)
{
// Declare necessary stream and writer objects
MemoryStream m = new MemoryStream();
XmlTextWriter xtw = new XmlTextWriter(m, null);
// Load the content into the writer
if (format == "html")
{
web.LoadHtmlAsXml(url, xtw);
// Rewind the memory stream
m.Position = 0;
// Create, fill, and return the xml document
XmlDocument xdoc = new XmlDocument();
string content = (new StreamReader(m)).ReadToEnd();
HtmlDocument doc = new HtmlDocument();
doc.OptionOutputAsXml = true;
doc.LoadHtml(content);
if (absolutizeLinks == true)
{
AttributeReferenceAbsolutizer.ExecuteDefaultAbsolutization
(doc.DocumentNode, url);
}
xdoc.LoadXml(doc.DocumentNode.OuterHtml);
return xdoc;
}
else
{
HtmlDocument doc = web.Load(url);
doc.OptionOutputAsXml = true;
XmlDocument xdoc = new XmlDocument();
if (absolutizeLinks == true)
{
AttributeReferenceAbsolutizer.ExecuteDefaultAbsolutization
(doc.DocumentNode, url);
}
xdoc.LoadXml(doc.DocumentNode.OuterHtml);
return xdoc;
}
}
示例8: GetHtmlNode
public HtmlNode GetHtmlNode()
{
var doc = new HtmlDocument();
doc.LoadHtml(DomHtml);
doc.Tag = this;
var req = Requests.First(x => x.IsFirst);
if (RedirectUrl != null) req = Requests.FirstOrDefault(x => x.Url == DomUrl);
if (req != null)
{
#if NATIVE_HTTP
if (req.ResponseHeaders == null) throw new System.Net.WebException("Unknown navigation error.");
#else
if (req.ResponseHeaders == null) throw new System.Net.Reimpl.WebException("Unknown navigation error.");
#endif
foreach (var header in req.ResponseHeaders)
{
doc.DocumentNode.SetAttributeValue("header-" + header.Key, header.Value);
}
}
foreach (var cook in Cookies)
{
doc.DocumentNode.SetAttributeValue("cookie-" + cook.Key, cook.Value);
}
doc.SetPageUrl(DomUrl);
doc.DocumentNode.SetAttributeValue("date-retrieved", DateTime.UtcNow.ToString("o"));
doc.DocumentNode.SetAttributeValue("requested-url", RequestedUrl);
foreach (var noscript in doc.DocumentNode.DescendantsAndSelf("noscript").ToList())
{
noscript.Remove();
}
return doc.DocumentNode;
}
示例9: RetrieveAccessCode
private static string RetrieveAccessCode(string authorizationUrl)
{
Process.Start("iexplore", authorizationUrl);
string appKey = null;
var shellWindows = new ShellWindows();
var htmlDocument = new HtmlDocument();
var ie = GetInternetExplorerInstance(shellWindows);
var document = ie.Document as HTMLDocument;
if (document != null)
{
HtmlNode elementById = null;
while (document.readyState != "complete" || elementById == null || !ie.LocationURL.Contains("accounts.google.com/o/oauth2/approval"))
{
htmlDocument.LoadHtml(document.documentElement.innerHTML);
elementById = htmlDocument.GetElementById("code");
Thread.Sleep(500);
}
appKey = elementById.Attributes["value"].Value;
}
ie.Quit();
return appKey;
}
示例10: CreateNode
/// <summary>
/// Creates an HTML node from a string representing literal HTML.
/// </summary>
/// <param name="html">The HTML text.</param>
/// <returns>The newly created node instance.</returns>
public static HtmlNode CreateNode(string html)
{
// REVIEW: this is *not* optimum...
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
return doc.DocumentNode.FirstChild;
}
示例11: LoadDocument
private static HtmlDocument LoadDocument(HTMLDocument document)
{
var htmlDocument = new HtmlDocument();
do
htmlDocument.LoadHtml(document.documentElement.innerHTML);
while (document.readyState != "complete");
return htmlDocument;
}