How to find the id of <input type= text> and if not present then add it in C#
I have considered the above link for my solution, but my problem is a bit different. Suppose in future I added a new textbox (id= "Answer4") to my string prior to the 1st textbox (id= "Answer1") like:
string Question="<input data-size="0" data-type="0" id="Answer4" placeholder="Answer4" type="text" />
<br/>
<br/>
<input data-size="0" data-type="0" id="Answer1" placeholder="Answer1" type="text" />
<br/>
<br/>
<input data-size="0" data-type="0" placeholder="Answer2" type="text" /><br/>
<br/>
<input data-size="0" data-type="0" id="Answer3" placeholder="Answer3" type="text" /><br/>
<br/>
";
and id is missing from the 2nd textbox. How can I add its id. I have done the code like :
string textboxTagPattern= @"(<input)([^>]*)(type=\"")(text)(\"")([^>]*)(/>)";
Regex rgx = new Regex(textboxTagPattern, RegexOptions.IgnoreCase);
MatchCollection questionInputText = rgx.Matches(Question);
var arr = Question.Replace("<input", "^<input").Split('^');
string ques = "";
int count = 0;
for (int i = 0; i < arr.Length; i++)
{
//If it is of <input type="text"> format and the id is not present
if (arr[i].IndexOf("type=\"text\"") != -1)
{
count++;
if (arr[i].IndexOf("id") == -1)
{
arr[i] = arr[i].Insert(arr[i].IndexOf("input ") + "input ".Length, "id=\"Answer"
+ count + "\" ");
// According to this logic the 2nd and 3rd textboxes will have the same
//id(id=Answer3), which is wrong. How can I correct it?
}
}
ques = ques + arr[i].Replace("\n", "");
}