We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

HTMLElement.ReplaceChild: Aspose.Html.Dom.DOMException: 'The object can not be found

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!

Ha, found the issue here! I confused the order, it must be

                    input.ParentElement.ReplaceChild( span, input );

instead of

                    input.ParentElement.ReplaceChild( input, span );

@jscharr

Please feel free to create a new topic in case you face any other issues.