Serialize xml output to class

Asked

Viewed 80 times

0

I have the following list of XML data:

<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 intended to serialize it so that it would be easier to read the data. To do so, 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 data list (since more than one result can come in XML)? I also need to add a field in my class for parent nodes (e.g. emails)?

  • The following link shows you how to do this: http://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110). aspx

2 answers

1


To make 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 made.

0

Apparently it’s 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);
  • I already got it started. I have some changes, like: XmlSerializer serializer = new XmlSerializer(typeof(EmailViewModel), new XmlRootAttribute("xml")); and yet StringReader rdr = new StringReader(responseBody);&#xA; EmailViewModel a = (EmailViewModel)serializer.Deserialize(rdr);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.