c# - HTML 敏捷包 : create html text node

标签 c# html-agility-pack

在 HtmlAgilityPack 中,我想创建 HtmlTextNode ,这是一个 HtmlNode (继承自 HtmlNode)具有自定义 InnerText。

HtmlTextNode CreateHtmlTextNode(string name, string text)
{
     HtmlDocument doc = new HtmlDocument();
     HtmlTextNode textNode = doc.CreateTextNode(text);
     textNode.Name = name;
     return textNode;
}

问题是 textNode.OuterHtmltextNode.InnerHtml在上述方法之后将等于“文本”。

例如CreateHtmlTextNode("title", "blabla")将产生: textNode.OuterHtml = "blabla"而不是 <Title>blabla</Title>

有没有更好的方法来创建HtmlTextNode

最佳答案

下面几行创建了一个包含内容的外部 html

var doc = new HtmlDocument();

// create html document
var html = HtmlNode.CreateNode("<html><head></head><body></body></html>");
doc.DocumentNode.AppendChild(html);

// select the <head>
var head = doc.DocumentNode.SelectSingleNode("/html/head");

// create a <title> element
var title = HtmlNode.CreateNode("<title>Hello world</title>");

// append <title> to <head>
head.AppendChild(title);

// returns Hello world!
var inner = title.InnerHtml;

// returns <title>Hello world!</title>
var outer = title.OuterHtml;

希望对您有所帮助。

关于c# - HTML 敏捷包 : create html text node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882626/

相关文章:

c# - 需要了解这个WPF程序中的事件触发。 .

c# - 如何获取目录上次修改日期=文本框日期并获取此目录中的文件c#

c# - 解析 HTML 以获取脚本变量值

c# - 在 Xamarin 中使用 HtmlAgilityPack 等待 AJAX

c# WPF datagrid数据绑定(bind)错误

C#/WPF : Binding Combobox ItemSource in Datagrid to element outside of the DataContext

pdf - 如何使用 iText 将带有图像和超链接的 HTML 转换为 PDF?

html-agility-pack - 解决StackOverflowException

c# - 为什么 Monitor.PulseAll 在信号线程中导致 "stepping stair"延迟模式?

c# - 从具有特定类名的元素中选择值