简体   繁体   English

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

[英]Delete nodes from returned from response.content using deserialization in RestSharp

I know the Response.Content from my GET RestRequest is xml UTF-8. 我知道我的GET RestRequest中的Response.Content是xml UTF-8。 However I want to delete unwanted child nodes from the response.content. 但是我想从response.content中删除不需要的子节点。 I can't assign the response to an xml document (because it returns a string apparently ?) which I could use system.xml to delete unwanted child nodes. 我无法将响应分配给xml文档(因为它显然返回了一个字符串?),因此可以使用system.xml删除不需要的子节点。

I have a situation (below) where if I have three space_reservation nodes [1401, 1402, and 1401 & 1402], I want to delete the space_reservation nodes for MH-1402 and (MH1401 & MH-1402), and keep the space_reservation node for MH-1401. 我有一个情况(如下),如果我有三个space_reservation节点[1401、1402和1401&1402],我想删除MH-1402和(MH1401&MH-1402)的space_reservation节点,并保留space_reservation节点适用于MH-1401。 I want to do this before I use the a REST API to schedule the rooms because these room will present a duplicate schedule situation. 在使用REST API安排房间之前,我想这样做,因为这些房间会出现重复的安排情况。

Here is the example of the response.content from the request: 这是来自请求的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>

Here are my deserialization classes: 这是我的反序列化类:

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; }
}

Here is the code I have for eliminating nodes that have title 1401. I save to xml so I can confirm that the node has been deleted but still the same. 这是我用来消除标题为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");
    }
}

} }

Im looking for the proper way to delete these nodes using RestSharp ? 我正在寻找使用RestSharp删除这些节点的正确方法吗?

The proper way of transforming structure of responses in RestSharp would be implementing a custom IDeserializer . 在RestSharp中转换响应结构的正确方法是实现自定义IDeserializer

However, it seems you want to apply business logic (ie remove some reservations) instead of just deserializing. 但是,您似乎要应用业务逻辑 (即删除一些保留)而不是仅反序列化。 In that case, you should use the already-deserialized object as shown in the example below. 在这种情况下,您应该使用已反序列化的对象,如下例所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM