[英]Remove whole div with specific class name
Is it possible to remove the whole div with a specific class name? 是否可以删除具有特定类名的整个div? For example;
例如;
<body>
<div class="head">...</div>
<div class="container">...</div>
<div class="foot">...</div>
</body>
I would like to remove the div with the "container" class. 我想使用“容器”类删除div。
AC# code example would be verry useful, thank you. AC#代码示例非常有用,谢谢。
If you want to parse html in c# the best way is to use Html agility pack : 如果要使用C#解析html,最好的方法是使用HTML敏捷包:
https://htmlagilitypack.codeplex.com/ https://htmlagilitypack.codeplex.com/
HtmlDocument document = new HtmlDocument();
document.Load(@"C:\yourfile.html")
HtmlNode nodesToRemove= document .DocumentNode.SelectNodes("//div[@class='container']").ToList();
foreach (var node in nodesToRemove)
node.Remove();
The proper way (I suppose) to do this is via built in Gecko DOM classes and methods. 正确的方法(我想)是通过内置的Gecko DOM类和方法实现的。
So, in your case something like: 因此,在您的情况下,例如:
var containers = yourDocument.GetElementsByClassName("container");
//this returns an IEnumerable of elements with this class. If you only ever gonna have one, you can do it like that:
var yourContainer = containers.FirstOrDefault();
yourContainer.Parent.RemoveChild(yourContainer);
Obviously, you can also do loops etc. 显然,您也可以执行循环等。
Well, with the help of regex, you can remove your desired div 好吧,借助正则表达式,您可以删除所需的div
var data = "<body>\n<div class=\"head\">...</div>\n" +
"<div class=\"container\">...</div>\n" +
"<div class=\"foot\">...</div>\n</body>";
var rxStr = "<div[^<]+class=([\"'])container\\1.*</div>";
var rx = new System.Text.RegularExpressions.Regex (rxStr,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
var nStr = rx.Replace (data, "");
Console.WriteLine (nStr);
This will reduce your string to 这将减少您的字符串
<body>
<div class="head">...</div>
<div class="foot">...</div>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.