Modify HTML file in a c sharp class

后端 未结 4 886
Happy的楠姐
Happy的楠姐 2021-01-20 21:17

First of all my application is not a web application. My aim is reading html files and modify them.

So that I write this code:

    string fileName =         


        
相关标签:
4条回答
  • 2021-01-20 21:54

    Get position of start and end index of section_1

    stringIndex =StringNthOccFinder.IndexOfNth(HtmlSting, "<Section_1", 0, 1);
    endIdex = StringNthOccFinder.IndexOfNth(HtmlSting, "</Section_1>", 0, 14);
    

    get HTML section which you want to remove

      HtmlSection= result.Substring(stringIndex , endIdex - stringIndex );
    

    Replace html section to ""

       result = result.Replace(HtmlSection, "");
    
    0 讨论(0)
  • 2021-01-20 21:56

    You can load the HTML as XML using XElement etc, but ultimately if you are removing or inserting characters you will need to resize the file and relocate the characters.

    0 讨论(0)
  • 2021-01-20 22:01

    Install the HAP package:

    Install-Package HtmlAgilityPack -Version 1.11.27
    

    String:

    // From String
    string result ="<div id="RemoveableDiv"></div> <div id="2"></div>"
    

    Code:

    var doc = new HtmlDocument();
    doc.LoadHtml(result);
    foreach (var node in doc.DocumentNode
    .Descendants("div")
    .Where(d => d.GetAttributeValue("Id", "RemoveableDiv").IndexOf("NavContent") >= 0)
    .ToArray())
    node.Remove();
    
    result = doc.documentnode.innerhtml;
    
    0 讨论(0)
  • 2021-01-20 22:11

    If you are wanting to just change certain things, you could be able to do this with XPath. Using the HTML Agility Pack, you can do this using C#.

    http://htmlagilitypack.codeplex.com/

    0 讨论(0)
提交回复
热议问题