使用RestSharp中的反序列化删除从response.content返回的节点

时间:2016-04-22 23:28:17

标签: c# xml restsharp

我知道我的GET RestRequest的Response.Content是xml UTF-8。但是,我想从response.content中删除不需要的子节点。我无法将响应分配给xml文档(因为它显然会返回一个字符串?),我可以使用system.xml删除不需要的子节点。

我有一个情况(下面),如果我有三个space_reservation节点[1401,1402和1401& 1402],我想删除MH-1402和(MH1401& MH-1402)的space_reservation节点,并保留MH-1401的space_reservation节点。我想在使用REST API来安排房间之前这样做,因为这些房间会出现重复的日程安排情况。

以下是请求中的response.content示例:

<?xml version="1.0" encoding="UTF-8"?>
<r25:reservations xmls:xsi="http://www.w3.org/2001/XMLSchemainstance">    
<r25:reservation xl:href="reservation.xml?rsrv_id=731397">
<r25:reservation_id>7313</r25:reservation_id>
<r25:reservation_state>1</r25:reservation_state>
<r25:event_start_dt>2016-04-12T09:00:00-07:00</r25:event_start_dt>
<r25:event_end_dt>2016-04-12T12:00:00-07:00</r25:event_end_dt>
<r25:event_id xl:href="event.xml?event_id=197559">197559</r25:event_id>
<r25:event_locator>2016-ABAHZP</r25:event_locator>
<r25:event_name>Spring Grand Rounds</r25:event_name>
<r25:event_type_name>Department Meetings & Events</r25:event_type_name>
<r25:organization_name>Sciences</r25:organization_name>
<r25:profile_name>April 12th Capture</r25:profile_name>
<r25:space_reservation xl:href="space.xml?space_id=335">
<r25:space_name>MH-1401</r25:space_name>
<r25:space_id>335</r25:space_id>
<r25:space_instruction_id>94367</r25:space_instruction_id>
</r25:space_reservation>
<r25:space_reservation xl:href="space.xml?space_id=336">
<r25:space_name>MH-1402</r25:space_name>
<r25:space_id>336</r25:space_id>
<r25:space_instruction_id>94368</r25:space_instruction_id>
</r25:space_reservation>
<r25:space_reservation xl:href="space.xml?space_id=337">
<r25:space_name>MH-1401 & 1402</r25:space_name>
<r25:space_id>337</r25:space_id>
<r25:space_instruction_id>94366</r25:space_instruction_id>
</r25:space_reservation>
<r25:resource_reservation xl:href="resource.xml?resource_id=55">
<r25:resource_id>55</r25:resource_id>
<r25:resource_name>Live plus on-demand</r25:resource_name>
<r25:resource_count>1</r25:resource_count>
<r25:resource_instruction_id/>
<r25:resource_instructions/>
</r25:resource_reservation>
</r25:reservation>
</r25:reservations>

以下是我的反序列化类:

public class Mreservation : List<reservation> { }

public class reservation
{

    public string event_name { get; set; }
    public DateTime reservation_start_dt { get; set; }
    public DateTime reservation_end_dt { get; set; }
    public DateTime event_start_dt { get; set; }
    public DateTime event_end_dt { get; set; }
    public string event_locator { get; set; }
    public int organization_id { get; set; }
    public List<space_reservation> spaceNodes { get; set; }
    public List<resource_reservation> resourceNodes { get; set; }
}

public class reservations
{
    public string pubdate { get; set; }
    public List<reservation> ReservationNodes { get; set; }
}

public class space_reservation
{
    public string space_name { get; set; }
    public int space_id { get; set; }
}

public class resource_reservation
{
    public int resource_id { get; set; }
    public string resource_name { get; set; }
}

这是我用于删除标题为1401的节点的代码。我保存到xml,以便我可以确认该节点已被删除但仍然相同。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
using System.Xml;

namespace EliminateDuplicates
{
class Program
{
    static void Main(string[] args)
    {
        var R25Client = R25_Rest_Login.R25Login();
        String testDate = new DateTime(2016, 4, 12).ToString("yyyyMMdd");
        var CaptureRequest = new RestRequest("reservations.xml",    Method.GET);
        CaptureRequest.AddParameter("resource_query_id", "35304");
        CaptureRequest.AddParameter("start_dt", testDate);
        CaptureRequest.AddParameter("end_dt", testDate);
        CaptureRequest.RequestFormat = DataFormat.Xml;
        var CaptureResponse = R25Client.Execute<Mreservation>(CaptureRequest);
        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(CaptureResponse.Content);
        xdoc.Save("beforeRemoval.xml");
        foreach (var x in CaptureResponse.Data)
        {
            if ((x.spaceNodes[0].space_id == 335) && (x.spaceNodes[1].space_id == 336) && (x.spaceNodes[2].space_id == 337))
            {
                x.spaceNodes.RemoveAll(i => i.space_id == 335);
            }
        }
        XmlDocument xdocA = new XmlDocument();
        xdocA.LoadXml(CaptureResponse.Content);
        xdocA.Save("afterRemoval.xml");
    }
}

}

我正在寻找使用RestSharp删除这些节点的正确方法吗?

1 个答案:

答案 0 :(得分:2)

在RestSharp中转换响应结构的正确方法是实现自定义IDeserializer

但是,您似乎想要应用business logic(即删除一些保留)而不仅仅是反序列化。在这种情况下,您应该使用已经反序列化的对象,如下例所示。

var reservations = client.Execute<Mreservation>(request).Data;
foreach(var reservation in reservations)
{
    reservation.SpaceNodes.RemoveAll((space) => someCondition(space));
}
// ...go on using reservations