It is just a case agreement issue.
To fix it, insert a
[XmlType("customer")]
above the line
public class Customer
----
If you want you can also work from the other direction.
Just Add() a few instances of Customer() to the Collection, then serialize
the collection (rather than serialize). Compare the serialized result with
the data you have. You'll see the disagreement pretty quickly.
-Dino
--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ OmitThis . m i c r o s o f t . c o m
"Brian Orrell" <brian.orrell@REMOVETHISTEXTmethodexperts.com> wrote in
message news:OI0XbuFOEHA.4036@TK2MSFTNGP12.phx.gbl...[color=blue]
> I know how to deserialize a collection class that is referenced in a
> container class as a variable or property using the XmlArray and
> XmlArrayItem attributes. What I can't for the life of me figure out is[/color]
how[color=blue]
> to deserialize a collection class that is the top level element of an xml
> stream. Hopefully the following example will make sense.
>
> The example below compiles and runs, but it doesn't pick up any of the
> children of the collection class. In other words, customers.Count = 0.
>
> Any and all help would be GREATLY appreciated.
>
> Thanks,
> Brian Orrell
> MethodExperts, LLC
>
http://www.methodexperts.com
>
brian.orrell@REMOVETHISTEXTmethodexperts.com
>
> If I have the following xml:
>
> <customers>
> <customer id="1" name="Bob" />
> <customer id="2" name="Sally" />
> <customer id="3" name="Joe" />
> </customers>
>
> And the following classes:
>
> using System;
> using System.Collections;
> using System.Collections.Specialized;
> using System.IO;
> using System.Reflection;
> using System.Xml;
> using System.Xml.Serialization;
> using System.Windows.Forms;
>
> namespace XmlSerializerTest
> {
> public class App
> {
> public static void Main(string[] args)
> {
> CustomerCollection customers;
> using (Stream stream =
>[/color]
Assembly.GetExecutingAssembly().GetManifestResourc eStream("XmlSerializerTest[color=blue]
> .Customers.xml"))
> {
> XmlTextReader reader = new XmlTextReader(stream);
> XmlSerializer serializer = new[/color]
XmlSerializer(typeof(CustomerCollection),[color=blue]
> new XmlRootAttribute("customers"));
> customers = (CustomerCollection)serializer.Deserialize(reader) ;
> stream.Close();
> }
>
> MessageBox.Show(customers.Count.ToString());
> }
>
> public class CustomerCollection : NameObjectCollectionBase, IEnumerable
> {
> public CustomerCollection()
> {
> }
>
> public void Add(Customer customer)
> {
> BaseAdd(customer.Name, customer);
> }
>
> public Customer this[int index]
> {
> get { return (Customer)BaseGet(index); }
> }
>
> public new IEnumerator GetEnumerator()
> {
> return BaseGetAllValues(typeof(Customer)).GetEnumerator() ;
> }
>
> }
>
> public class Customer
> {
> public Customer()
> {
> }
>
> [XmlAttribute("id")]
> public int Id;
>
> [XmlAttribute("name")]
> public string Name;
> }
> }
> }
>
>
>
> --
> Brian Orrell
> MethodExperts, LLC
>
brian.orrell@REMOVETHISTEXTmethodexperts.com
>
>
>
>
> --
> Brian Orrell
> MethodExperts, LLC
>
brian.orrell@REMOVETHISTEXTmethodexperts.com
>
>[/color]