c# - How can I deserialize an XML response to an object when I only need a child element of the XML? -
i have next xml getting webrequest:
<?xml version="1.0" encoding="utf-8"?> <search> <total_items>5</total_items> <page_size>10</page_size> <page_count>1</page_count> <page_number>1</page_number> <page_items></page_items> <first_item></first_item> <last_item></last_item> <search_time>0.044</search_time> <events> <event id="e0-001-053379749-0"> <title>antibalas</title> <url>http://test.com</url> <description>stuff</description> <start_time>2013-01-30 20:00:00</start_time> <stop_time></stop_time> <tz_id></tz_id> <tz_olson_path></tz_olson_path> <tz_country></tz_country> <tz_city></tz_city> <venue_id>v0-001-000189211-5</venue_id> <venue_url>http://blah.com</venue_url> <venue_name>troubadour</venue_name> <venue_display>1</venue_display> <venue_address>9081 santa monica boulevard</venue_address> <city_name>west hollywood</city_name> <region_name>california</region_name> <region_abbr>ca</region_abbr> <postal_code>90069</postal_code> <country_name>united states</country_name> <country_abbr2>us</country_abbr2> <country_abbr>usa</country_abbr> <latitude>34.0815917</latitude> <longitude>-118.3892462</longitude> <geocode_type>evdb geocoder</geocode_type> <all_day>0</all_day> <recur_string></recur_string> <calendar_count></calendar_count> <comment_count></comment_count> <link_count></link_count> <going_count></going_count> <watching_count></watching_count> <created>2012-12-24 11:40:43</created> <owner>evdb</owner> <modified>2013-01-14 21:08:04</modified> <performers> <performer> <id>p0-001-000000517-4</id> <url>http://test.com</url> <name>antibalas</name> <short_bio>afro-beat / funk / experimental</short_bio> <creator>jfisher</creator> <linker>evdb</linker> </performer> </performers> <image> <url>http://s4.evcdn.com/images/small/i0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url> <width>48</width> <height>48</height> <caption></caption> <thumb> <url>http://s4.evcdn.com/images/thumb/i0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url> <width>48</width> <height>48</height> </thumb> <small> <url>http://s4.evcdn.com/images/small/i0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url> <width>48</width> <height>48</height> </small> <medium> <url>http://s4.evcdn.com/images/medium/i0-001/000/070/311-5.jpeg_/antibalas-11.jpeg</url> <width>128</width> <height>128</height> </medium> </image> <privacy>1</privacy> <calendars></calendars> <groups></groups> <going></going> </event> </events> </search>
i have created next classes:
public class eventsearch { public int total_items { get; set; } public int page_size { get; set; } public int page_count { get; set; } public int page_number { get; set; } public int page_items { get; set; } public string first_item { get; set; } public string last_item { get; set; } public string search_time { get; set; } public list<event> events { get; set; } } public class event { public string id { get; set; } public string title { get; set; } public string url { get; set; } public string description { get; set; } public datetime start_time { get; set; } public string venue_id { get; set; } public string venue_url { get; set; } public string venue_name { get; set; } public string venue_address { get; set; } public string city_name { get; set; } public string region_name { get; set; } public string region_abbr { get; set; } public string postal_code { get; set; } public string country_name { get; set; } public string country_abbr { get; set; } public string country_abbr2 { get; set; } public double latitude { get; set; } public double longitude { get; set; } public list<performer> performers { get; set; } public eventimage image { get; set; } } public class performer { public string id { get; set; } public string url { get; set; } public string name { get; set; } public string short_bio { get; set; } } public class eventimage { public string url { get; set; } public int width { get; set; } public int height { get; set; } public image thumb { get; set; } public image little { get; set; } public image medium { get; set; } } public class image { public string url { get; set; } public int width { get; set; } public int height { get; set; } }
in method calling, want homecoming list of events. here's have far, giving me error:
not expected.
code:
var request = webrequest.create(url); var response = request.getresponse(); if (((httpwebresponse)response).statuscode == httpstatuscode.ok) { // stream containing content returned server. stream datastream = response.getresponsestream(); // open stream using streamreader. streamreader reader = new streamreader(datastream); xmlserializer serializer = new xmlserializer(typeof(eventsearch)); eventsearch deserialized = (eventsearch)serializer.deserialize(reader); homecoming deserialized.events; }
i'm not sure next. need annotate classes?
yes, need add together attributes
classes
map xml elements objects.
this should work, had alter few things, datetime in xml in wrong fromat made string(you can parse needed), page_items
int
empty element in xml, wont able deserialize this, have alter string if has chance of beingness empty.
[xmltype("search")] public class eventsearch { public int total_items { get; set; } public int page_size { get; set; } public int page_count { get; set; } public int page_number { get; set; } // public int? page_items { get; set; } public string first_item { get; set; } public string last_item { get; set; } public string search_time { get; set; } public list<event> events { get; set; } } [xmltype("event")] public class event { [xmlattribute("id")] public string id { get; set; } public string title { get; set; } public string url { get; set; } public string description { get; set; } public string start_time { get; set; } public string venue_id { get; set; } public string venue_url { get; set; } public string venue_name { get; set; } public string venue_address { get; set; } public string city_name { get; set; } public string region_name { get; set; } public string region_abbr { get; set; } public string postal_code { get; set; } public string country_name { get; set; } public string country_abbr { get; set; } public string country_abbr2 { get; set; } public double latitude { get; set; } public double longitude { get; set; } public list<performer> performers { get; set; } public eventimage image { get; set; } } [xmltype("performer")] public class performer { public string id { get; set; } public string url { get; set; } public string name { get; set; } public string short_bio { get; set; } } [xmltype("image")] public class eventimage { public string url { get; set; } public int width { get; set; } public int height { get; set; } public image thumb { get; set; } public image little { get; set; } public image medium { get; set; } } [xmltype("thumb")] public class image { public string url { get; set; } public int width { get; set; } public int height { get; set; } }
i tested straight file using:
using (filestream stream = new filestream("c:\\test.xml", filemode.open)) { xmlserializer serializer = new xmlserializer(typeof(eventsearch)); eventsearch deserialized = (eventsearch)serializer.deserialize(stream); }
but stream
webresponce
should work same filestream
hope helps :)
c# xml serialization webrequest
No comments:
Post a Comment