2

I am working on an asp.net website.I need to replace a particular string in the html using c#.The following is the html. Here i need to replace "@name" by a valid name using c# code. I tried using java script.It's working.How can i achieve this using c#?

How can i get the Current Page's HTML using c# or HtmlAgilityPack in order to parse it?

HTML:

<div>
In the @name, you may have configured an iPad in both the AppleDevices and the TabletDevices configuration. However, because AppleDevices may have been set for a small display size, you want an iPad to use the TableDevices configuration (which has a larger screen). Reorder the devices in the following order so that an iPad will use the TableDevices configuration first.
Tablet Devices
Apple Devices
</div>
Bisileesh
  • 1,982
  • 2
  • 19
  • 29

5 Answers5

3
var result = html.Replace("@name", valid_name)
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61
3

The simplest way is to use String.Replace(String, String) method:

string newString = html.Replace("@name", "valid name");
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
  • How can i load the current page's html to HtmlDocument of HtmlAgilitypack? – Bisileesh Jul 12 '12 at 12:46
  • @NewBornDeveloper - Either use `HtmlWeb` to load from the web (it takes a URL), or `HtmlDocument` that will load from a local path. – Oded Jul 12 '12 at 13:18
  • I tried using var hw = new HtmlWeb(); HtmlDocument doc = hw.Load(Request.Url.ToString()); But no luck! – Bisileesh Jul 12 '12 at 13:36
2

Assuming this is MVC take a look at my CsQuery project. CsQuery is a jQuery port and CSS selector engine, which you can use to work with the HTML directly. But more importantly the project includes an example with code to access the HTML for a page before it renders in C# under MVC.

Accessing partial views is pretty easy, see Rick Strahl's blog post on the subject.

If you want to access the entire HTML of a page and potentially alter it before it's rendered, however, you need to create a custom ViewEngine, and make callbacks to the controller where you will be able to access the HTML. There's quite a bit involved in doing this right. Rather than copy hundreds of lines of code, take a look at the example MVC app included with CsQuery, specifically the classes in the CsQueryView folder:

https://github.com/jamietre/CsQuery/tree/master/examples/CsQuery.MvcApp

This includes a custom view engine and a custom Controller base class that lets you add methods to controllers like this:

// runs for all actions
public void Cq_Start()
{
    Doc["a.not-allowed"]
        .Attr("onclick","javascript:alert('You're not authorized to click this')");
}

// runs for the Index action
public void Cq_Index()
    Doc["div"].Css("border", "1px solid red;");
}

These methods are called after the regular action methods that correspond, and set the value of Doc. Doc is a CQ object (the core object in CsQuery). This contains all the HTML for a page. It's like a jQuery object. In your situation you could just use jQuery methods like:

// select all divs on the page
var div = Doc["div"];

// do parameter substitution
var newText = div.Text().Replace("@name", valid_name);

// update the text
div.Text(newText);

To switch your MVC app to use this view engine you need to add this code to Application_Start:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CsQueryViewEngine());

If don't want to use CsQuery, though, the example should show you how to access the HTML output in MVC before it renders. It uses reflection to figure out the methods to call back in your controller, and it could easily be adapted to provide a string of the HTML instead of a CsQuery object.

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
  • I don't know how to contact with you, I think this place is the best way to contact you. I made a project in Java using Jsoup, that parses the HTML and make DOM tree, and that I used in various operations, just like, comparing templates of two URLs. But the problem is Jsoup does not get dynamic contents of the given HTML(URL). So how can I do that using Jsoup/CSquery? I saw your project in git. But there no tutorials/examples. So please help me to solve my problem. My questions are , http://stackoverflow.com/questions/15805976/how-to-get-dynamic-contents-in-dom-tree-using-jsoup-in-java – devsda Apr 04 '13 at 10:05
  • http://stackoverflow.com/questions/15718235/optimized-algorithm-to-compare-templates-of-two-urls – devsda Apr 04 '13 at 10:06
  • CsQuery is a .NET library and Jsoup is a Java library. Which environment are you using? Also I apologize that the documentation is a little weak, but there are two example projects in the CsQuery git repository under the `examples` folder and the documentation covers the basics of loading HTML; most of the functionality mirrors jQuery if you need more details on using jQuery methods just see the jQuery documentation. Also if you just search on the `csquery` tag on Stack Overflow there are many Q&A's with specific examples for a lot of common uses. – Jamie Treworgy Apr 04 '13 at 13:26
  • Ohk, I am using Netbeans , and maven. Then can you tell me how can I get the dynamic page. I have one idea, `WebDriver driver = new FirefoxDriver(); driver.get(url1);` run these commands (using Selenium) and store content in file/string then parse using Jsoup (that part has been already done by me). Can you tell me how can I copy dynaic content using selenium. – devsda Apr 04 '13 at 13:36
  • Sorry, I can't help you. CsQuery is only for .NET. – Jamie Treworgy Apr 04 '13 at 14:04
  • Is CSquery can get Dynamic content as selenium do ? `WebDriver driver = new FirefoxDriver(); driver.get("URL"); String page_content = driver.getPageSource();` It takes most of the time of the whole proccessing of algorithm. Is CSquery can able to do all this work in efficient way ? [ dynamic content fetching ] – devsda Apr 04 '13 at 16:12
  • CsQuery does NOT work like selenium -it's only an HTML parser and CSS selection engine. It can be used WITH selenium, though. – Jamie Treworgy Apr 04 '13 at 20:45
1
string htmlContent=@"<div>In the @name, you may have configured an iPad in both the AppleDevices and the TabletDevices configuration. However, because AppleDevices may have been set for a small display size, you want an iPad to use the TableDevices configuration (which has a larger screen). Reorder the devices in the following order so that an iPad will use the TableDevices configuration first.
Tablet Devices
Apple Devices
</div>";
string htmlNewContent=htmlContent.Replace("@name",valid_name);
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
0

In case is just this replacement, you can go with string.Replace();

Your code seems like a html teplate. If your list of variables will grow, I strongly recommend to use Razor as template engine, in which you get static typing, intellisense in you html tables and other features.

Tomas Grosup
  • 6,396
  • 3
  • 30
  • 44