Ir para o conteúdo

Exemplo de Script – Retornar um Campo Como XML

Este script de exemplo de plug-in retornará um campo em seu objeto de dados por meio do endpoint REST como XML, tornando-o consumível por outros aplicativos e plataformas. Veja as Notas de Configuração para implementar este Plugin no Vinyl.

Exemplo de Script

#r "Newtonsoft.Json.dll"
using System;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
/**
 The format of the json produced by Vinyl is:
 {
     items: [
       {
           "field1": "value",
           "field2": "value",
           "field3": "value",
           ...
       }
       ...
     ]
 }

 This plugin is grabbing the FIRST item returned (["items"][0])

 We then look for a particular field containing xml - in this case "xmlCode"

 We then return this as the xml content - discarding the rest of the message
**/
// Read the response content
var message = await Response.Content.ReadAsStringAsync();
var jobject = JObject.Parse(message);
// This is likely always the same - the first item returned
var item = jobject["items"][0];
// Change xmlCode to match the name of the field in your object:
var xmlContent = item["xmlCode"].ToString();
// Return just the xmlContent
Response.Content = new StringContent(xmlContent, null, "application/xml");

Notas de Configuração

  1. Ao configurar o Plugin no Vinyl, utilize os seguintes valores:

    • Nome: ReturnXmlFromJson
    • Descrição: Escolha o xml retornado de dentro do json e retorne apenas como xml
    • Objetivo: Resposta de Descanso
  2. Ao configurar o XMP Endpoint no Vinyl, clique em Edge Case Settings e defina o valor Endpoint Plugin Response como ReturnXmlFromJson

  3. Ao configurar o serviço Web da API REST, defina o valor Request Content Type como JSON e Response Content Type como XML