473,748 Members | 10,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serializing XML Attributes in Arrays

1 New Member
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 1243

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

Similar topics

2
2132
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 entry to the task collection property (of Document). public TaskCollection Tasks {
2
3298
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 ‘DataItem’ public class DataItem { public DataItem() {}
0
1332
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 an error when serializing. Can this be made without redeclaring the ArrayList special attributes? The problem is that the class B is in different namespace!
4
2732
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 am using to create a sample XML file. I am trying to dimension the ArrayOfJudgeEntity to have two sets of the JudgeTableEntity values. When I run the code I get an error that the XML is not correct. I jsut can't get my head around the array...
3
3169
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 the server (W2K3) it throws an exception. It only seems to happen when serializing/deserializing _arrays_ of a type. If I just serialize/deserialize one instance, it works fine. The exception I get is: (sorry for the word wrapping.)...
4
3620
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 writing an accounting application. I have a chart of accounts in the form of a containment hierarchy. A GeneralLedger contains a number of Accounts, and each of these Accounts can contain a Aubledger, which contains its own Accounts, and so on. The...
4
1676
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 on the impact serializing something has on an asp.net page rendering, compared to say, hitting the db or something like that. THanks Jason Shohet
0
2230
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 <XmlRoot()_ Public Class Foo ' I want the key/value pairs to be xml attributes of the root.
6
3003
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 serialized array. This works flawlessly when using only single line text. When I tried to store multiline text, the problem arose. When the serialized data is deserialized, the array breaks. Any suggestions?
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.