I need to escape special characters in an invalid XML file which is about 5000 lines long. Here\'s an example of the XML that I have to deal with:
name & surname
is not well-formed XML. It should be:
name & surname
All conformant XML tools should create this - you normally do not have to worry. If you create a string with the '&' character then an XML tool will output the escaped version. If you create the string by hand it is your responsibility to make sure it is escaped. If you use an XML editor it should escape it for you.
If the file has been given you by someone else, send it back and tell them it is not well-formed. If they no longer exist you will have to use a plain text editor. That's fragile and messy but there is no other way. If the file has ampersands elsewhere that are used for escaping then the file is garbage.
See a 10-year-old post here and a later one here.