本文整理汇总了C#中HtmlAgilityPack.HtmlDocument.get_DocumentNode方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument.get_DocumentNode方法的具体用法?C# HtmlDocument.get_DocumentNode怎么用?C# HtmlDocument.get_DocumentNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HtmlAgilityPack.HtmlDocument
的用法示例。
在下文中一共展示了HtmlDocument.get_DocumentNode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CleanHtml
public static string CleanHtml(string rawHtml, TrustedHtmlLevel level)
{
if (string.IsNullOrEmpty(rawHtml))
{
return rawHtml;
}
HtmlDocument htmlDocument = new HtmlDocument
{
OptionAutoCloseOnEnd = true,
OptionWriteEmptyNodes = true
};
TrustedHtml trustedHtml = DIContainer.Resolve<TrustedHtml>();
switch (level)
{
case TrustedHtmlLevel.Basic:
trustedHtml = trustedHtml.Basic();
break;
case TrustedHtmlLevel.HtmlEditor:
trustedHtml = trustedHtml.HtmlEditor();
break;
}
htmlDocument.LoadHtml(rawHtml);
HtmlNodeCollection htmlNodeCollection = htmlDocument.get_DocumentNode().SelectNodes("//*");
if (htmlNodeCollection != null)
{
string host = string.Empty;
if (HttpContext.Current != null)
{
host = WebUtility.HostPath(HttpContext.Current.Request.Url);
}
System.Collections.Generic.Dictionary<string, string> enforcedAttributes;
htmlNodeCollection.ToList<HtmlNode>().ForEach(delegate(HtmlNode n)
{
if (trustedHtml.IsSafeTag(n.get_Name()))
{
n.get_Attributes().ToList<HtmlAttribute>().ForEach(delegate(HtmlAttribute attr)
{
if (!trustedHtml.IsSafeAttribute(n.get_Name(), attr.get_Name(), attr.get_Value()))
{
attr.Remove();
return;
}
if (attr.get_Value().StartsWith("javascirpt:", System.StringComparison.InvariantCultureIgnoreCase))
{
attr.set_Value("javascirpt:;");
}
});
enforcedAttributes = trustedHtml.GetEnforcedAttributes(n.get_Name());
if (enforcedAttributes != null)
{
foreach (System.Collections.Generic.KeyValuePair<string, string> current in enforcedAttributes)
{
if (!(
from a in n.get_Attributes()
select a.get_Name()).Contains(current.Key))
{
n.get_Attributes().Add(current.Key, current.Value);
}
else
{
n.get_Attributes().get_Item(current.Key).set_Value(current.Value);
}
}
}
if (n.get_Name() == "a" && n.get_Attributes().Contains("href"))
{
string value = n.get_Attributes().get_Item("href").get_Value();
if (value.StartsWith("http://") && !value.ToLowerInvariant().StartsWith(host.ToLower()))
{
if (!(
from a in n.get_Attributes()
select a.get_Name()).Contains("rel"))
{
n.get_Attributes().Add("rel", "nofollow");
return;
}
if (n.get_Attributes().get_Item("rel").get_Value() != "fancybox")
{
n.get_Attributes().get_Item("rel").set_Value("nofollow");
return;
}
}
}
}
else
{
if (trustedHtml.EncodeHtml)
{
n.set_HtmlEncode(true);
return;
}
n.RemoveTag();
}
});
}
return htmlDocument.get_DocumentNode().WriteTo();
}
示例2: GetHtmlNode
public static string GetHtmlNode(string html, string xpath)
{
if (string.IsNullOrEmpty(html))
{
return html;
}
HtmlDocument htmlDocument = new HtmlDocument
{
OptionAutoCloseOnEnd = true,
OptionWriteEmptyNodes = true
};
htmlDocument.LoadHtml(html);
HtmlNode htmlNode = htmlDocument.get_DocumentNode().SelectSingleNode(xpath);
if (htmlNode == null)
{
return string.Empty;
}
return htmlNode.get_OuterHtml();
}
示例3: GetHtmlNodes
public static System.Collections.Generic.List<string> GetHtmlNodes(string html, string xpath)
{
if (string.IsNullOrEmpty(html))
{
return null;
}
HtmlDocument htmlDocument = new HtmlDocument
{
OptionAutoCloseOnEnd = true,
OptionWriteEmptyNodes = true
};
htmlDocument.LoadHtml(html);
HtmlNodeCollection htmlNodeCollection = htmlDocument.get_DocumentNode().SelectNodes(xpath);
if (htmlNodeCollection == null)
{
return null;
}
return (
from n in htmlNodeCollection
select n.get_OuterHtml()).ToList<string>();
}
示例4: CloseHtmlTags
public static string CloseHtmlTags(string html)
{
if (string.IsNullOrEmpty(html))
{
return html;
}
HtmlDocument htmlDocument = new HtmlDocument
{
OptionAutoCloseOnEnd = true,
OptionWriteEmptyNodes = true
};
htmlDocument.LoadHtml(html);
return htmlDocument.get_DocumentNode().WriteTo();
}