2013-11-27 44 views
0

我需要在C#中插入一個不存在於字符串中的關閉「input」元素。插入關閉「input」HTML元素

例如:

This is some content <input type="readonly" value="value1"> and 
there is further content. This is some additional <input type="readonly" 
value="value2"> text and then there is further text. 

必須與具有關閉「輸入」元素來替換:

This is some content <input type="readonly" value="value1"></input> and 
there is further content. This is some additional <input type="readonly" 
value="value2"></input> text and then there is further text. 

注意會有開閉「輸入」的元件之間沒有內容,它只要打開「輸入」元素結束,只需要提供一個關閉「輸入」元素。

+2

'input'沒有一個封閉的標記。它只是'' – MusicLovingIndianGirl

+0

@Aishvarya:你甚至不需要'/>'在void元素上。 http://webdesign.about.com/od/html5tags/f/html5-use-trailing-slash.htm –

+0

可以通過關閉「/ input」標籤進行「輸入」。它是有效的,這就是我想要的,因爲在解析期間,它期望結束標記。 – user2751130

回答

0

下面是代碼,我相信有更優雅的方式,但在這裏它是

static void AddClosingTag() 
{ 
    string input = "This is some content <input type='readonly' value='value1'> and there is further content. This is some additional <input type='readonly' value='value2'> text and then there is further text."; 

    int index = 0; 

    while (true) 
    { 
     index = input.IndexOf("<input", index); 
     if (index == -1) 
     { 
      break; 
     } 
     index = input.IndexOf(">", index); 
     input = input.Insert(index + 1, "</input>"); 
    } 

    Console.WriteLine(input); 
} 
+0

也許你可以使用'.IndexOf(「 Ben

+0

謝謝,它工作正常。 – user2751130

+0

@ user2751130,所以請標記爲答案 –