2013-02-19 3 views
2

나는 내가 WebRequest 클래스에서 다시 얻고 다음 XML했다 : 나는 다음과 같은 클래스를 생성 한XML의 자식 요소 만 필요로 할 때 객체에 대한 XML 응답을 비 직렬화 할 수 있습니까?

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

: 나는를 반환 내가 부르고 방법에

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

을 원하는 이벤트 목록. 다음은 나에게 오류를 제공하는, 지금까지이 작업은 다음과 같습니다

was not expected.

코드 :

var request = WebRequest.Create(url); 
var response = request.GetResponse(); 

if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK) 
{ 
    // Get the stream containing content returned by the server. 
    Stream dataStream = response.GetResponseStream(); 
    // Open the stream using a StreamReader. 
    StreamReader reader = new StreamReader(dataStream); 

    XmlSerializer serializer = new XmlSerializer(typeof(EventSearch)); 
    EventSearch deserialized = (EventSearch)serializer.Deserialize(reader); 

    return deserialized.events; 
} 

나는 다음에 무엇을 모르겠어요. 내 수업에 주석을 달아야합니까?

+0

이 도움이 FileStream

희망과 동일하게 작동합니다 :

[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 small { 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; } } 

나는 사용하여 파일에서 직접 테스트 속성과 요소 사이. 예를 들어, 이벤트는'id' 요소를 가지고 있지 않은 것처럼 보입니다. 그들은'id' 속성을 가지고 있습니다. MSDN을 참조하십시오. http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx –

답변

3

예, classesAttributes을 추가하여 xml 요소를 개체에 매핑해야합니다.

이 작동합니다

, 그러나 나는 몇 가지를 변경했다, XML에서 날짜 시간이 잘못된 fromat에 그래서 나는 그것이 문자열, 또한 page_itemsint입니다 (필요에 따라 구문 분석 할 수 있습니다)했지만 그것은이다 xml의 ​​빈 요소는 비 직렬화 할 수 없으며 비어있을 가능성이있는 경우 문자열로 변경해야합니다.

using (FileStream stream = new FileStream("C:\\Test.xml", FileMode.Open)) 
{ 
    XmlSerializer serializer = new XmlSerializer(typeof(EventSearch)); 
    EventSearch deserialized = (EventSearch)serializer.Deserialize(stream); 
} 

그러나 Webresponce에서 Stream을 당신은 구별 할 필요가 :

+0

완벽하게 작동했습니다! 고마워요 sa_ddam213. – MattSavage

관련 문제