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