473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 1198

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

Similar topics

2
by: Werner B. Strydom | last post by:
Hi I wrote a class, Document, which has an Array of ITask objects. In order to serialize the tasks contained within the tasks array, I marked each task class with an attribute , and added an...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
0
by: Ante Smolcic | last post by:
Hi all, I have an ArrayList that contains items of type A. I declared the XmlArrayItem atribute for that type. Now I have an derived type B (from A) also contained in the ArrayList but I get...
4
by: Wayne Wengert | last post by:
I am still stuck trying to create a Class to use for exporting and importing array data to/from XML. The format of the XML that I want to import/export is shown below as is the Class and the code I...
3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
4
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm...
4
by: Jason Shohet | last post by:
We are thinking of serializing an object & passing it toseveral functions on web service. This will happen about 35 times as the page loads. The class has about 20 attributes. We're not sure...
0
by: aeden.jameson | last post by:
Hi, I was wondering if somone could provide an outline of how I would go about implementing serialization that turns a string dictionary to XML attributes. For example, suppose I have ...
6
by: dawnerd | last post by:
Hello everyone. I have a question, or problem if you will, that I'm sure someone knows the answer to. I have a database that stores information on a given user. The information is stored in a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.