473,320 Members | 1,867 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,320 software developers and data experts.

deserializing xml data in an object

Hello,

I have an xml stream like :

<biblio>
<books>
<book>
<title>book 01</title>
<editor>toto</editor>
</book>
<book>
<title>book 02</title>
<editoreur>titi</editor>
</book>
</books>
</biblio>

I have an object Book. What kind of code do I use to have instances of Book
object with this Xml stream ?

thanks for your help

Steph

Nov 11 '05 #1
1 3538
Steph wrote:
I have an xml stream like :

<biblio>
<books>
<book>
<title>book 01</title>
<editor>toto</editor>
</book>
<book>
<title>book 02</title>
<editoreur>titi</editor>
</book>
</books>
</biblio>

I have an object Book. What kind of code do I use to have instances of Book
object with this Xml stream ?


I'm not sure I understood exactly what do you need, but here is some code I
hope will help you:

public class Book {
public string title;
public string editor;
}

[XmlRoot("biblio")]
public class Biblio {
[XmlArray("books")]
[XmlArrayItem("book", typeof(Book))]
public Book[] Books;
}

public class Test {
static void Main(string[] args) {
string xml=
@"<biblio>
<books>
<book>
<title>book 01</title>
<editor>toto</editor>
</book>
<book>
<title>book 02</title>
<editor>titi</editor>
</book>
</books>
</biblio>";
XmlSerializer serializer = new XmlSerializer(typeof(Biblio));
Biblio catalog = (Biblio)serializer.Deserialize(new StringReader(xml));
foreach (Book book in catalog.Books) {
Console.WriteLine("\"{0}\" by {1}", book.title, book.editor);
}
}
}

The result is:
"book 01" by toto
"book 02" by titi

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Armstrong | last post by:
I'm having difficulties deserializing some objects. Consider the following example of what I'm trying to do: ---------------------------------------------------------- class Person { string...
0
by: Kenneth Baltrinic | last post by:
I am getting the following error when deserializing an object that has a couple of dozen dependant objects in its object graph. Anyone who can suggest where I might begin to look to resolve problem...
3
by: Mark McConnell | last post by:
Regarding deserializing XML into a custom object... I've been able to deserialize an XML doc into my custom object and everything works great. The problem I am encountering is when one of the...
4
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in...
5
by: Lucas | last post by:
Well, here's the situation.. It's pretty simple; just that I can't get it to work. I have 2 Executables.. The first one is called CSharp.exe which is a simple WinForm App. I have a single...
0
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the...
2
by: Phillip Galey | last post by:
I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also have a collection object called Places, which is...
4
by: Andrew E Chapman | last post by:
What happens to an object which is not nothing has another object deserialized into it? I assume that the data in it is completely destroyed by the new object. I am interested because I have...
2
by: Chris | last post by:
I need to serialise a business object into a session as a string. The reason for this is that we have to run session state with classic ASP and the code we use in effect mean ASP.net sessions can...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.