2012-07-12 92 views
1

我正在一個asp.net網站上工作。我需要使用c#替換HTML中的特定字符串。以下是html。 在這裏,我需要用一個有效的名稱替換「@name」使用C#代碼。 我嘗試使用Java腳本。它的工作。我可以如何實現這一點使用C#?使用c的HTML解析#

如何獲取當前頁面的HTML使用c#或HtmlAgilityPack爲了解析它?

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> 
+0

「我怎麼能得到當前頁面的HTML」你還沒有告訴我們,如果你使用WebForms,MVC,NancyFX ... – 2012-07-12 14:23:09

回答

1

假設這是MVC,請看看我的CsQuery項目。 CsQuery是一個jQuery端口和CSS選擇器引擎,您可以使用它直接處理HTML。但更重要的是,該項目包含一個代碼,用於在MVC下的C#中呈現之前訪問頁面的HTML。

訪問部分視圖非常容易,請參閱Rick Strahl的blog post

但是,如果您想要訪問整個頁面的HTML並可能在呈現之前對其進行更改,則需要創建自定義ViewEngine,並對控制器進行回調,以便您可以訪問HTML。做這件事很有意義。而不是複製幾百行代碼的,看看附帶CsQuery MVC應用的例子中,特別是在CsQueryView文件夾中的類:

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

這包括自定義視圖引擎和一個自定義Controller基類這可以讓你添加方法的控制器是這樣的:

// 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;"); 
} 

這些方法稱爲對應的常規操作方法,並設置Doc值。 Doc是一個CQ對象(CsQuery中的核心對象)。這包含一個頁面的所有HTML。它就像一個jQuery對象。在你的情況,你可以只使用jQuery的方法,如:

// 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); 

要切換MVC應用程序使用,你需要將此代碼添加到Application_Start這個視圖引擎:

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

如果不想使用CsQuery,但該示例應該向您展示如何在呈現之前訪問MVC中的HTML輸出。它使用反射來找出在你的控制器中回調的方法,並且它可以很容易地用來提供一串HTML而不是一個CsQuery對象。

+0

我不知道如何與您聯繫,我認爲這個地方是與您聯繫的最佳方式。我使用Jsoup製作了一個使用Jsoup的項目,解析HTML並製作DOM樹,並用於各種操作,就像比較兩個URL的模板一樣。但問題是Jsoup沒有獲得給定HTML(URL)的動態內容。那麼我該如何使用Jsoup/CSquery來做到這一點?我在git中看到了你的項目。但沒有教程/例子。所以請幫我解決我的問題。 我的問題是,http://stackoverflow.com/questions/15805976/how-to-get-dynamic-contents-in-dom-tree-using-jsoup-in-java – devsda 2013-04-04 10:05:34

+0

http://stackoverflow.com/questions/15718235 /優化算法對比較模板 - 的 - 兩個網址 – devsda 2013-04-04 10:06:16

+0

CsQuery是一個.NET庫,Jsoup是一個Java庫。你在使用哪種環境?另外,我很抱歉文檔有點弱,但在'examples'文件夾下的CsQuery git存儲庫中有兩個示例項目,文檔涵蓋了加載HTML的基礎知識;如果您需要更多關於使用jQuery方法的詳細信息,請參閱jQuery文檔,大多數功能都反映了jQuery。另外,如果您只是在Stack Overflow上搜索'csquery'標記,那麼很多常見問題的示例中都有許多問答。 – 2013-04-04 13:26:36

3
var result = html.Replace("@name", valid_name) 
+0

我怎樣才能加載當前頁面的HTML到HtmlAgilitypack的HtmlDocument? – Bisileesh 2012-07-12 12:49:33

+0

http://stackoverflow.com/q/5599012/1475234 - 這個環節有類似Question.But無法得到當前頁面的HTML。 – Bisileesh 2012-07-12 13:39:47

3

最簡單的方法是使用String.Replace(String, String)方法:

string newString = html.Replace("@name", "valid name"); 
+0

如何將當前頁面的html加載到HtmlAgilitypack的HtmlDocument中? – Bisileesh 2012-07-12 12:46:53

+0

@NewBornDeveloper - 要麼使用'HtmlWeb'從網絡上(它需要一個URL),或'HtmlDocument'將從本地路徑加載加載。 – Oded 2012-07-12 13:18:29

+0

我試過用var hw = new HtmlWeb(); 的HTMLDocument DOC = hw.Load(Request.Url.ToString());但沒有運氣! – Bisileesh 2012-07-12 13:36:27

0

如果僅僅是這種替換,你可以去與string.replace();

您的代碼看起來像一個html teplate。 如果您的變量列表將增長,我強烈建議使用Razor作爲模板引擎, 其中您可以在html表格和其他功能中獲得靜態類型,intellisense。

1
string [email protected]"<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);