Question: An error occurred when I ran the function DeleteBadCharacters to fix my file

when I open my worksheet it pop-up an error like this:

There were problems during the loading process, Your worksheet may become incomplete.

and I try to fix my file use this method: https://www.mapleprimes.com/questions/129377-There-Were-Problems-During-The-Loading?reply=reply , but an error occurred as follows .

And my code is:

restart;

DeleteBadCharacters := proc(file :: string)
local base, badchar, char, cnt, msg, outfile, str, unicode;
    str := FileTools:-Text:-ReadFile(file);
    for cnt from 0 do
        try
            XMLTools:-ParseString(str);
            break;
        catch "An invalid XML character":
            msg := lastexception[2];
            if not StringTools:-RegMatch("Unicode: 0x([^)]+)", msg, 'all', 'unicode') then
                error;
            end if;
            unicode := sscanf(unicode,"%x");
            char := convert(unicode,'bytes');
            badchar[cnt+1] := char;
            str := StringTools:-SubstituteAll(str, char, "");
        end try;
    end do;

    if cnt=0 then
        printf("no errors in file\n");
    else
        if not StringTools:-RegMatch("^(.*)\\.mw$", file, 'all', 'base') then
            error "problem extracting basename";
        end if;
        printf("deleted bad characters: %A\n", {seq(badchar[cnt],cnt=1..cnt)});
        outfile := sprintf("%s-fixed.mw", base);
        FileTools:-Text:-WriteString(outfile, str);
        fclose(outfile);
        printf("wrote updated file to %s\n", outfile);
    end if;
    return NULL;
end proc:
NULL;
NULL;
DeleteBadCharacters( "E:/EchoModel_V3.mw" );

And the error is

"Error, (in XMLTools:-ParseString) XML document structures must start and end within the same entity."

I use maple 2021.  Thank you very much!

Please Wait...