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

XML Serialization of Top Level Collection Class

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 how
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
br**********@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 =
Assembly.GetExecutingAssembly().GetManifestResourc eStream("XmlSerializerTest
..Customers.xml"))
{
XmlTextReader reader = new XmlTextReader(stream);
XmlSerializer serializer = new XmlSerializer(typeof(CustomerCollection),
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
br**********@REMOVETHISTEXTmethodexperts.com


--
Brian Orrell
MethodExperts, LLC
br**********@REMOVETHISTEXTmethodexperts.com
Nov 12 '05 #1
1 3124
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" <br**********@REMOVETHISTEXTmethodexperts.com> wrote in
message news:OI**************@TK2MSFTNGP12.phx.gbl...
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 how 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
br**********@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 =
Assembly.GetExecutingAssembly().GetManifestResourc eStream("XmlSerializerTest .Customers.xml"))
{
XmlTextReader reader = new XmlTextReader(stream);
XmlSerializer serializer = new XmlSerializer(typeof(CustomerCollection), 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
br**********@REMOVETHISTEXTmethodexperts.com


--
Brian Orrell
MethodExperts, LLC
br**********@REMOVETHISTEXTmethodexperts.com

Nov 12 '05 #2

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

Similar topics

3
by: Franz | last post by:
Let me describe the flow of my program first. 1. Deserialize data from xml file. 2. Addition of "PersonType" class to the AllPersonalData. 3. Serialize data back to the xml file. My question is...
0
by: Joe Rizla | last post by:
I am using XML Serialization to output the IBuySpy tabs data. I have used System.Xml.Serialization to serialize an array of a class called TabStripDetails. Using attributes I rename the resultant...
2
by: Just D. | last post by:
All, 1. Did anybody write Serialization/Deserialization of some custom class derived from the CollectionBase class? The custom class is like a container of many different simple classes, each...
2
by: Ethem Azun | last post by:
Hi, I have a collection class called Printers that implement the ICollection and IEnumerable. This class contains a private ArrayList that holds the Printer objects. The printer class is a...
27
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the...
1
by: Alex Clark | last post by:
Hi all, Apologies for the cross-post but I can't determine if this is a VS .NET problem or a VB.NET language issue. I'm using .NET 1.1 SP1, VS 2003 EA, VB.NET. I'm coding a custom component...
3
by: GreyAlien007 | last post by:
I extended the class TreeNode to add some properties of my liking. Anyway, no problems there, I can add my derived TreeNode into TreeNodeCollections and use the properties etc. However, when I...
0
by: nobin01 | last post by:
Dear sir; I want ur Help in serialization.I know serialization.I Know binary,soap and xmlserialization also.But i want ur help in following topics.pls help me as soon as possible.I have search in...
2
by: Peter Duniho | last post by:
I've been learning about mechanisms .NET offers to export data. The initial goal is to see what sorts of ways are available to save an application's state (document, internal database, whatever). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.