469,923 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Serializing XML Attributes in Arrays

I'm trying to make an object that will reflect the response from isbndb.com's API so I can serialize the results into my code. Right now I have code that works, however, the structure isn't quite the same as what isbndb.com uses.

My problem, is serializing the array of BookData objects and putting attributes into the array wrapper tag <BookList>. Right now, I'm just using a custom object called BookList to add attributes to the element, but this only allows for 1 <BookData> tag inside of it, which will not work if I get back an array of BookData elements.

I'd like to have an object called ISBNdb, with an array of BookData objects inside of it and have the attributes "total_results", etc. get put inside of the BookList tag as attributes.

This is the code I have right now:

Expand|Select|Wrap|Line Numbers
  1. public class ISBNdb
  2. {
  3.     [XmlAttribute]
  4.     public string server_time
  5.     {
  6.         get { return _server_time.ToString("u"); }
  7.         set { _server_time = DateTime.Parse(value); }
  8.     }
  9.     [XmlIgnore]
  10.     private DateTime _server_time;
  11.  
  12.     public BookList BookList;
  13. }
  14.  
  15. public class BookList
  16. {
  17.     [XmlAttribute]
  18.     public uint total_results;
  19.     [XmlAttribute]
  20.     public uint page_size;
  21.     [XmlAttribute]
  22.     public uint page_number;
  23.     [XmlAttribute]
  24.     public uint shown_results;
  25.     public BookData BookData;
  26. }
  27.  
  28. public class BookData
  29. {
  30.     [XmlAttribute]
  31.     public string book_id;
  32.     [XmlAttribute]
  33.     public string isbn;
  34.     public string Title;
  35.     public string TitleLong;
  36.     public string AuthorsText;
  37.     public PublisherText PublisherText;
  38. }
  39.  
  40. public class PublisherText
  41. {
  42.     [XmlText]
  43.     public string Publisher;
  44.     [XmlAttribute]
  45.     public string publisher_id;
  46. }


Below is the XML response I get back from isbndb.com
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <ISBNdb server_time="2007-12-23T13:54:18Z">
  4.     <BookList total_results="1" page_size="10" page_number="1" shown_results="1">
  5.         <BookData book_id="to_kill_a_mockingbird_a02" isbn="0060194995">
  6.             <Title>To kill a mockingbird</Title>
  7.             <TitleLong></TitleLong>
  8.             <AuthorsText>Harper Lee</AuthorsText>
  9.             <PublisherText publisher_id="harpercollinspublishers">New York : HarperCollinsPublishers, 1995.</PublisherText>
  10.         </BookData>
  11.     </BookList>
  12. </ISBNdb>
I want to have code that looks more like this:

Expand|Select|Wrap|Line Numbers
  1. public class ISBNdb
  2. {
  3.     [XmlAttribute]
  4.     public string server_time
  5.     {
  6.         get { return _server_time.ToString("u"); }
  7.         set { _server_time = DateTime.Parse(value); }
  8.     }
  9.     [XmlIgnore]
  10.     private DateTime _server_time;
  11.  
  12.     [XmlAttribute]
  13.     public uint total_results;
  14.     [XmlAttribute]
  15.     public uint page_size;
  16.     [XmlAttribute]
  17.     public uint page_number;
  18.     [XmlAttribute]
  19.     public uint shown_results;
  20.  
  21.     [XmlArray]
  22.     public BookData[] BookList;
  23.  
  24.  
  25. }
  26.  
  27.  
  28. public class BookData
  29. {
  30.     [XmlAttribute]
  31.     public string book_id;
  32.     [XmlAttribute]
  33.     public string isbn;
  34.     public string Title;
  35.     public string TitleLong;
  36.     public string AuthorsText;
  37.     public PublisherText PublisherText;
  38. }
  39.  
  40. public class PublisherText
  41. {
  42.     [XmlText]
  43.     public string Publisher;
  44.     [XmlAttribute]
  45.     public string publisher_id;
  46. }
I want to be able to have the properties for total_results, page_size, etc. be serialized as attributes like the XML from isbndb.com so that it will deserialize for me automatically.

My main issue with my code right now is that arrays don't work right now and they should. Regardless of what my code ends up looking like, I want to be able to get back the BookData as an array so that I am reflecting what isbndb.com is sending me.
Dec 23 '07 #1
0 1055

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Werner B. Strydom | last post: by
2 posts views Thread by Aleksei Guzev | last post: by
reply views Thread by Ante Smolcic | last post: by
4 posts views Thread by Wayne Wengert | last post: by
4 posts views Thread by Dave Veeneman | last post: by
4 posts views Thread by Jason Shohet | last post: by
reply views Thread by aeden.jameson | last post: by
6 posts views Thread by dawnerd | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.