1
votes

i want to know how to remove :

<?xml version="1.0" encoding="UTF-8"?>

from a string data. I have tried this but it doesn't work

string result = data.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", "");

(I am not working with xml , it's just a response to manipulate it without header )

1
It looks like you're processing XML. Have you looked at using XmlReader, XElement or XmlDocument for processing that, rather than raw string operations? Assuming that this processing directive will literally be present in a document is not very reliable. - Jeroen Mostert
Thnx for the reply, but I'm not processing XML, it's a string to show the content without the xml header - Zied R.
The string parameter in your replace command has an extra space before the closing ?> which is not in your sample data - Tian van Heerden
@ZiedRebhi: if what you've got is not XML, then it's interesting how it starts with an XML declaration. You don't often see random strings like that. :-) - Jeroen Mostert
it's a response as a string , i need to delelte only this line - Zied R.

1 Answers

3
votes

Let's look at your two strings. Removing the escapes they are:

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>

In other words you've managed to add an extra space. Remove that and your code will succeed.

More broadly, one wonders why you are attempting to do this. Simple text processing of XML files is liable to lead to pain and suffering. Perhaps you should consider using a parser.