Hi,
I get an exception “The object can not be found” at line “input.ParentNode.ReplaceChild( input, span );” when doing a HTMLElement.ReplaceChild like following:
private void Test()
{
string htmlString = "<!DOCTYPE html>\r\n<html>\r\n<body>\r\n\r\n<h2>HTML Forms</h2>\r\n\r\n" +
"<form action=\"/action_page.php\">\r\n " +
"<label for=\"fname\">First name:</label><br>\r\n " +
"<input type=\"text\" id=\"fname\" name=\"fname\" value=\"John\"><br>\r\n " +
"<label for=\"lname\">Last name:</label><br>\r\n " +
"<input type=\"text\" id=\"lname\" name=\"lname\" value=\"Doe\"><br>\r\n " +
"<label for=\"cars\">Choose a car:</label>\r\n\r\n" +
"<select name=\"cars\" id=\"cars\">\r\n " +
"<option value=\"volvo\">Volvo</option>\r\n " +
"<option value=\"saab\">Saab</option>\r\n " +
"<option value=\"mercedes\">Mercedes</option>\r\n " +
"<option value=\"audi\">Audi</option>\r\n" +
"</select><br><br>" +
"<input type=\"submit\" value=\"Submit\">\r\n</form> \r\n\r\n\r\n" +
"</body>\r\n</html>";
using ( var htmlDoc = new HTMLDocument( htmlString, "" ) )
{
HTMLCollection inputs = htmlDoc.GetElementsByTagName( "input" );
foreach ( Element input in inputs )
{
Element span = htmlDoc.CreateElement( "span" );
span.InnerHTML = ((HTMLInputElement)input).Value ?? string.Empty;
input.ParentNode.ReplaceChild( input, span );
}
}
}
What am I doing wrong? Shouldn’t the parent of a child always be able to find that child?
Thanks in advance!