OnBeforeDeserialization and IRestResponse.Content not persisting

1,103 views
Skip to first unread message

StrandedPirate

unread,
Jan 11, 2012, 5:03:38 AM1/11/12
to RestSharp
My third party vendor is sending duplicate xml elements in his rest
responses which is causing RestSharp to not parse the xml into my c#
classes. I'm doing the following to remove the duplicate xml elements
in the OnBeforeDeserialization action but the assignment of
IRestResponse.Content to the new xml that I've stripped the duplicates
from does NOT persist beyond the OnBeforeDeserialization method. What
am I doing wrong or how can I strip the duplicates from the Content
prior to deserialization? I really don't want to re-invent the wheel
with IDeserializer..


private static void RemoveDuplicateElement(XElement link,
string elementName)
{
XElement lastClickCommission =
link.Elements(elementName).LastOrDefault();
if (lastClickCommission != null)
lastClickCommission.Remove();
}

private void BeforeDeserialize(IRestResponse response)
{
XDocument doc = XDocument.Parse(response.Content);
IEnumerable<XElement> links = doc.Descendants("link");
foreach (XElement link in links)
{
RemoveDuplicateElement(link, "category");
RemoveDuplicateElement(link, "click-commission");
RemoveDuplicateElement(link, "creative-height");
RemoveDuplicateElement(link, "creative-width");
}
response.Content = doc.ToString(); // assign Content
property to the duplicate free xml
}

Andrew Young

unread,
Jan 11, 2012, 12:36:28 PM1/11/12
to rest...@googlegroups.com
Looking at the source you might need set RawBytes instead of the Content. The Content property doesn't get ported over to the generic instance of RestResponse.

Mike Denton

unread,
Sep 23, 2015, 8:51:08 AM9/23/15
to RestSharp
Even using RawBytes doesn't seem to modify the content...

I've tried doing a string.Replace() on the Content and RawBytes properties and neither change seems to have any effect...
Message has been deleted

Mike Denton

unread,
Sep 24, 2015, 8:49:09 AM9/24/15
to RestSharp
Altering either the Content or RawBytes properties has no effect on the output.

You must use a custom XML deserializer if you want to preprocess the content.

Fortunately this is actually quite easy as you can simply extend RestSharp.Deserializers.XmlDeserializer and override the Deserialize<T> method and do any pre-processing on response.Content before calling base.Deserialize<T>(response). This works like I expected it to work when I put it in OnBeforeDeserialize.
Reply all
Reply to author
Forward
0 new messages