Serialize xml output to class

0

I have the following XML data list:

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

And I wanted to Serialize it so it could be easier to read the data. For that, I created the following view model:

EmailViewModel.cs (edited)

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

Now the questions: How do I serialize this list of data (since more than one result can come in XML)? Do I also need to add a field in my class to the parent nodes (eg emails)?

    
asked by anonymous 28.07.2014 / 17:37

2 answers

1

To do the Serialization I had to make some changes. Starting with 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);

Where the variable a has the serialization done.

    
28.07.2014 / 18:55
0

Apparently everything is all right. To serialize, use the following:

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);
    
28.07.2014 / 18:47