由于另一个产品中的PHP错误,我有时会得到格式错误的XML响应,如下所示:
<?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)
在反序列化之前,是否存在清除此字符串的安全方法?
重复索赔是正确的,但链接的副本没有提供有效的解决方案。
当前的临时解决方案仅在起始字符串是有效XML且附加错误不包含与根标记匹配的另一个结束标记时才有效:
RegexOptions options = RegexOptions.Singleline | RegexOptions.Compiled;
var tidyStreamContents = Regex.Match(streamContents, @"^<\?xml.*?\?>\s*?<(.*?)>.*</(\1)>", options, Regex.InfiniteMatchTimeout).ToString();
答案 0 :(得分:0)
您可以使用CsQuery将无效的XML视为HTML,将其清理,然后输出为字符串以供进一步处理:
using CsQuery;
var cq = CQ.CreateFromFile("input.txt");
var sCleanXML = cq("customfields").RenderSelection;
输入(input.txt
的内容):
<?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)
输出(sCleanXML
的值):
<customfields> </customfields>
另一种方法是使用XmlReader或HtmlAgilityPack。