Due to a PHP error in another product I sometimes get an ill formed XML response, like this:
<?xml version="1.0" encoding="UTF-8"?>
<customfields>
</customfields>Warning
Router: https://example.com/api/index.php?/Tickets/TicketCustomField/Get
file_put_contents(./__swift/cache/SWIFT_Loader.cache): failed to open stream: Invalid argument (C:/Kayako/support/__swift/library/Loader/class.SWIFT_Loader.php:1630)
Does a safe method exist to clean up this string before I deserialize it?
The claim of duplicate is correct, but the linked duplicate does not give a working solution.
The current temporary solution, only works if the starting string is valid XML and the appended error does not contain another closing tag which matches the root tag:
RegexOptions options = RegexOptions.Singleline | RegexOptions.Compiled;
var tidyStreamContents = Regex.Match(streamContents, @"^<\?xml.*?\?>\s*?<(.*?)>.*</(\1)>", options, Regex.InfiniteMatchTimeout).ToString();