0

Tenho a seguinte lista de dados em XML:

<xml>
   <cnt>7469</cnt>
   <emails>
   <item>
      <to_email>[email protected]</to_email>
      <id>3352143303</id>
      <app_id>121746</app_id>
      <subject>[REF:GAR23004513_6470_0]</subject>
      <status>blocked</status>
      <sendtime_start>1406559567</sendtime_start>
      <sendtime_end>1406559567</sendtime_end>
      <from_id>2726590621</from_id>
      <from_name/>
      <from_email>[email protected]</from_email>
      <email_id>9158240961</email_id>
      <state_id>14</state_id>
      <to_id>1220492939</to_id>
      <cnt_recipients>1</cnt_recipients>
      <sent/>
      <open/>
      <click/>
      <bounce/>
      <spam/>
      <blocked>1</blocked>
      <queued>0</queued>
      <status_message>preblocked</status_message>
      </item>
   </emails>
   <status>OK</status>
</xml>

E pretendia Serializa-la para poder ser mais fácil ler os dados. Para tal, criei o seguinte view model:

EmailViewModel.cs (editado)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcTesteLayout.ViewModels
{
    [System.SerializableAttribute()]
    public class EmailViewModel
    {
        public string cnt { get; set; }

        public class emails {
            public item[] item;
        }

        public class item {
            public string to_email { get; set; }
            public string id { get; set; }
            public string app_id { get; set; }
            public string subject { get; set; }
            public string status { get; set; }
            public string sendtime_start { get; set; }
            public string sendtime_end { get; set; }
            public string from_id { get; set; }
            public string from_name { get; set; }
            public string from_email { get; set; }
            public string email_id { get; set; }
            public string state_id { get; set; }
            public string to_id { get; set; }
            public string sent { get; set; }
            public string open { get; set; }
            public string click { get; set; }
            public string bounce { get; set; }
            public string spam { get; set; }
            public string blocked { get; set; }
            public string queued { get; set; }
            public string status_message { get; set; }
        }
    }
}

Agora as dúvidas: Como faço para serializar esta lista de dados (visto que pode vir mais do que um resultado no XML)? Preciso também de adicionar um campo na minha class para os nós pais (ex. emails)?

1

2 Respostas 2

1

Para fazer a Serialização tive de fazer algumas alterações. Começando pelo EmailViewModel:

namespace MvcTesteLayout.ViewModels
{
    [System.SerializableAttribute()]
    public class EmailViewModel
    {
        public string cnt { get; set; }
        public item[] emails { get; set; }
    }

    public class item
    {
        public string to_email { get; set; }
        public string id { get; set; }
        public string app_id { get; set; }
        public string subject { get; set; }
        public string status { get; set; }
        public string sendtime_start { get; set; }
        public string sendtime_end { get; set; }
        public string from_id { get; set; }
        public string from_name { get; set; }
        public string from_email { get; set; }
        public string email_id { get; set; }
        public string state_id { get; set; }
        public string to_id { get; set; }
        public string sent { get; set; }
        public string open { get; set; }
        public string click { get; set; }
        public string bounce { get; set; }
        public string spam { get; set; }
        public string blocked { get; set; }
        public string queued { get; set; }
        public string status_message { get; set; }
    }
}

No Controller:

string responseBody = ((new StreamReader(ResponseStream)).ReadToEnd());
//Serializar
XmlSerializer serializer = new XmlSerializer(typeof(EmailViewModel),  new XmlRootAttribute("xml"));
StringReader rdr = new StringReader(responseBody);
EmailViewModel a = (EmailViewModel)serializer.Deserialize(rdr);

Onde a variável a tem a serialização feita.

0

Aparentemente está tudo certo. Para serializar, use o seguinte:

var emailViewModel = new EmailViewModel {
    cnt = 7469;
    emails = new emails {
        items = new item[] {
            new item {
                to_email = "[email protected]",
                id = 3352143303,
                app_id = 121746,
                subject = "[REF:GAR23004513_6470_0]",
                status = "blocked",
                sendtime_start = 1406559567,
                sendtime_end = 1406559567,
                from_id = 2726590621,
                from_email = "[email protected]",
                email_id = 9158240961,
                state_id = 14,
                to_id = 1220492939,
                cnt_recipients = 1,
                blocked = 1,
                queued = 0,
                status_message = "preblocked"
            }    
        }
    },
    status = "OK"
};

XmlSerializer x = new XmlSerializer(emailViewModel.GetType());
x.Serialize(Console.Out, emailViewModel);
1
  • Já consegui colocar a funcionar. Tenho algumas alterações, como: XmlSerializer serializer = new XmlSerializer(typeof(EmailViewModel), new XmlRootAttribute("xml")); e ainda StringReader rdr = new StringReader(responseBody); EmailViewModel a = (EmailViewModel)serializer.Deserialize(rdr); 28/07/2014 às 16:50

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .