我有一个TagBuilder,其中包含外部元素和内部元素。如何遍历“输入”级别行,并将以下内容添加为“新”属性?
<header>
<nav id="tf-menu" class="navbar navbar-default menu">
<div class="container menus">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand logo logo_1" href="index.html">
<img src="img/logo.jpg" style="height:110px; margin-left:50%;" class="hidden-xs hidden-sm"/>
<img src="img/logo-s.jpg" style="height:75px"class="visible-xs visible-sm"/>
</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<br class="visible-xs"/><br class="visible-xs"/>
<ul class="nav navbar-nav navbar-right menus_der">
<li><a href="router.html" v-if="personaLogueada">Mi Perfil</a></li>
<li><a href="routerf6f2.html?page=blog" class="btn btn-xs btn-warning btn-circle"><span class="fa fa-rss hidden-xs"></span><span class="visible-xs">Blog</span></a></li>
<li><a target="new" href="mailto:gestion@graphics.com" class="btn btn-xs btn-warning btn-circle"><span class="fa fa-envelope-o hidden-xs"></span><span class="visible-xs">Contacto</span></a></li>
</ul>
</div>
</div>
</nav></header>
placeholder="Search"
如果不存在Tagbuilder / Taghelper方法,是否应使用C#HTML Agility软件包来编辑标签树? https://html-agility-pack.net/traversing;如果是,如何将Tagbuilder /或Taghelper输出转换为Agility Package HTMLDocument?
*这与问题Add CSS Class to All Tags in TagBuilder, Edit Existing Attribute不同,因为它要求编辑现有属性。这里的问题是关于添加新属性。参见以下文章:
Why isn't it good to ask multiple questions and answers in one question
答案 0 :(得分:4)
对于您的任务,您可以使用HtmlAgilityPack。
使用 HtmlAgilityPack ,您可以使用 XPath查询选择必要的节点并将标签添加到该节点。
要选择节点,可以使用SelectNodes方法:
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var nodes = htmlDoc.DocumentNode.SelectNodes("//input[contains(@class, 'focusedOut')]");
要添加属性,您可以使用Attributes Collection:
node.Attributes.Add("placeholder","Search");