开发者

node.ReplaceChild()

开发者 https://www.devze.com 2023-02-20 08:34 出处:网络
How do you use the node.ReplaceChild() method to replace </td></tr><tr><td> tag with the <br /> tag when the <tr> t开发者_C百科ag only has 1 <td> tag?

How do you use the node.ReplaceChild() method to replace </td></tr><tr><td> tag with the <br /> tag when the <tr> t开发者_C百科ag only has 1 <td> tag?

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.OptionFixNestedTags = true; 
htmlDoc.Load(HttpContext.Current.Server.MapPath("~/myFolder/rpt.html").ToString());
if(htmlDoc.DocumentNode != null) 
{
   HtmlNode trNode = htmlDoc.DocumentNode.SelectSingleNode("//tr"); 
   if (trNode != null)
   {
      //check for more than 1 <td> in the same <tr> 
      bool td = trNode.ToString().StartsWith("</td><td"); 
      if (td == false)
      {
          trNode.ReplaceChild("</td></tr><tr><td>","<br />");
      }
   }
}


I don't think you can do that. </tr> is the closing tag for a <tr> node, not a node in itself. You'd need to process the two nodes involved together. Probably replace the first <tr> node with the combination that you're after, and then remove the second.

0

精彩评论

暂无评论...
验证码 换一张
取 消