Hello all,
I have two simple classes: Item and ItemCollection. Item stores a label for
the item and an instance of ItemCollection for all child items.
ItemCollection just stores a collection of Items. ItemCollection implements
System.Collecti ons.ICollection .
When I use XmlSerializer to serialize an instance of ItemCollection, I am
unable to control the element names used to contain the item collection and
the items therein. Applying XmlRootAttribut e to the ItemCollection class
causes an exception because it implements ICollection.
I have also tried implementing IXmlSerializer in my ItemCollection class.
However, this fails on two accounts: each Item's children are no longer
serialized and the root element is still out of my control.
Any help figuring out how to do this would be much appreciated.
Here is code you can run from the console:
////////////////////////////////////////////////////////////
using System;
using System.Collecti ons;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Seri alization;
class XmlTest {
[STAThread]
static void Main(string[] args) {
XmlSerializer xmlSerializer = new XmlSerializer(t ypeof(ItemColle ction));
StringBuilder sb = new StringBuilder() ;
//dummy ItemCollection to serialize
ItemCollection itemCollection = new ItemCollection( );
itemCollection. Add(new Item("First Item"));
itemCollection. Add(new Item("Second Item"));
itemCollection. Add(new Item("Third Item"));
itemCollection[2].Children.Add(n ew Item("First Child"));
itemCollection[2].Children.Add(n ew Item("Second Child"));
//serialize it
xmlSerializer.S erialize(new StringWriter(sb ), itemCollection) ;
//output the resultant XML
//NOTE THAT THE XML ELEMENTS THAT START WITH CAPITALS ARE OUT OF MY
CONTROL.
//I WANT TO BE ABLE TO CONTROL THEM
Console.Out.Wri teLine("XML:{0} {0}{1}{0}", Environment.New Line, sb);
}
}
[XmlRoot("item")]
public class Item {
private string _label;
private ItemCollection _children;
[XmlElement("lab el")]
public string Label {
get {
return _label;
}
set {
_label = value;
}
}
[XmlArray("child ren")]
[XmlArrayItem("c hild")]
public ItemCollection Children {
get {
return _children;
}
}
public Item() : this(string.Emp ty) {
}
public Item(string label) {
_label = label;
_children = new ItemCollection( );
}
}
public class ItemCollection : ICollection {
private ArrayList _items;
public int Count {
get {
return _items.Count;
}
}
public bool IsSynchronized {
get {
return _items.IsSynchr onized;
}
}
public object SyncRoot {
get {
return _items.SyncRoot ;
}
}
public Item this[int index] {
get {
return (Item) _items[index];
}
}
public ItemCollection( ) {
_items = new ArrayList();
}
public void Add(Item item) {
_items.Add(item );
}
public void CopyTo(Array array, int index) {
_items.CopyTo(a rray, index);
}
public IEnumerator GetEnumerator() {
return _items.GetEnume rator();
}
}
////////////////////////////////////////////////////////////
Thanks,
Kent 2 6810
You can use a root override when serializing. Would that help?
Example: http://www.winisp.net/cheeso/srcview...lectionBase.cs
-D
"Kent Boogaart" <ke****@interno de.on.net> wrote in message
news:ew******** ******@TK2MSFTN GP09.phx.gbl... Hello all,
I have two simple classes: Item and ItemCollection. Item stores a label
for the item and an instance of ItemCollection for all child items. ItemCollection just stores a collection of Items. ItemCollection
implements System.Collecti ons.ICollection .
When I use XmlSerializer to serialize an instance of ItemCollection, I am unable to control the element names used to contain the item collection
and the items therein. Applying XmlRootAttribut e to the ItemCollection class causes an exception because it implements ICollection.
I have also tried implementing IXmlSerializer in my ItemCollection class. However, this fails on two accounts: each Item's children are no longer serialized and the root element is still out of my control.
Any help figuring out how to do this would be much appreciated.
Here is code you can run from the console:
//////////////////////////////////////////////////////////// using System; using System.Collecti ons; using System.IO; using System.Text; using System.Xml; using System.Xml.Seri alization;
class XmlTest { [STAThread] static void Main(string[] args) { XmlSerializer xmlSerializer = new XmlSerializer(t ypeof(ItemColle ction)); StringBuilder sb = new StringBuilder() ; //dummy ItemCollection to serialize ItemCollection itemCollection = new ItemCollection( ); itemCollection. Add(new Item("First Item")); itemCollection. Add(new Item("Second Item")); itemCollection. Add(new Item("Third Item")); itemCollection[2].Children.Add(n ew Item("First Child")); itemCollection[2].Children.Add(n ew Item("Second Child")); //serialize it xmlSerializer.S erialize(new StringWriter(sb ), itemCollection) ; //output the resultant XML //NOTE THAT THE XML ELEMENTS THAT START WITH CAPITALS ARE OUT OF MY CONTROL. //I WANT TO BE ABLE TO CONTROL THEM Console.Out.Wri teLine("XML:{0} {0}{1}{0}", Environment.New Line, sb); } }
[XmlRoot("item")] public class Item { private string _label; private ItemCollection _children;
[XmlElement("lab el")] public string Label { get { return _label; } set { _label = value; } }
[XmlArray("child ren")] [XmlArrayItem("c hild")] public ItemCollection Children { get { return _children; } }
public Item() : this(string.Emp ty) { }
public Item(string label) { _label = label; _children = new ItemCollection( ); } }
public class ItemCollection : ICollection { private ArrayList _items;
public int Count { get { return _items.Count; } }
public bool IsSynchronized { get { return _items.IsSynchr onized; } }
public object SyncRoot { get { return _items.SyncRoot ; } }
public Item this[int index] { get { return (Item) _items[index]; } }
public ItemCollection( ) { _items = new ArrayList(); }
public void Add(Item item) { _items.Add(item ); }
public void CopyTo(Array array, int index) { _items.CopyTo(a rray, index); }
public IEnumerator GetEnumerator() { return _items.GetEnume rator(); } }
////////////////////////////////////////////////////////////
Thanks, Kent
Thanks Dino,
That helps a little but doesn't solve the entire problem. Using a root
override allows me to specify the root (obviously) element but not the
element used for each individual collection item (as the ArrayItemAttrib ute
does). I've read elsewhere that it is not possible to do so at this stage.
Hence, I have implemented a wrapper class whose sole purpose is to
facilitate XML serialization of my ItemCollection class. Nasty, but it
works.
Regards,
Kent
"Dino Chiesa [Microsoft]" <di****@online. microsoft.com> wrote in message
news:eS******** *****@TK2MSFTNG P10.phx.gbl... You can use a root override when serializing. Would that help?
Example: http://www.winisp.net/cheeso/srcview...lectionBase.cs -D
"Kent Boogaart" <ke****@interno de.on.net> wrote in message news:ew******** ******@TK2MSFTN GP09.phx.gbl... Hello all,
I have two simple classes: Item and ItemCollection. Item stores a label for the item and an instance of ItemCollection for all child items. ItemCollection just stores a collection of Items. ItemCollection implements System.Collecti ons.ICollection .
When I use XmlSerializer to serialize an instance of ItemCollection, I
am unable to control the element names used to contain the item collection and the items therein. Applying XmlRootAttribut e to the ItemCollection class causes an exception because it implements ICollection.
I have also tried implementing IXmlSerializer in my ItemCollection
class. However, this fails on two accounts: each Item's children are no longer serialized and the root element is still out of my control.
Any help figuring out how to do this would be much appreciated.
Here is code you can run from the console:
//////////////////////////////////////////////////////////// using System; using System.Collecti ons; using System.IO; using System.Text; using System.Xml; using System.Xml.Seri alization;
class XmlTest { [STAThread] static void Main(string[] args) { XmlSerializer xmlSerializer = new
XmlSerializer(t ypeof(ItemColle ction)); StringBuilder sb = new StringBuilder() ; //dummy ItemCollection to serialize ItemCollection itemCollection = new ItemCollection( ); itemCollection. Add(new Item("First Item")); itemCollection. Add(new Item("Second Item")); itemCollection. Add(new Item("Third Item")); itemCollection[2].Children.Add(n ew Item("First Child")); itemCollection[2].Children.Add(n ew Item("Second Child")); //serialize it xmlSerializer.S erialize(new StringWriter(sb ), itemCollection) ; //output the resultant XML //NOTE THAT THE XML ELEMENTS THAT START WITH CAPITALS ARE OUT OF MY CONTROL. //I WANT TO BE ABLE TO CONTROL THEM Console.Out.Wri teLine("XML:{0} {0}{1}{0}", Environment.New Line, sb); } }
[XmlRoot("item")] public class Item { private string _label; private ItemCollection _children;
[XmlElement("lab el")] public string Label { get { return _label; } set { _label = value; } }
[XmlArray("child ren")] [XmlArrayItem("c hild")] public ItemCollection Children { get { return _children; } }
public Item() : this(string.Emp ty) { }
public Item(string label) { _label = label; _children = new ItemCollection( ); } }
public class ItemCollection : ICollection { private ArrayList _items;
public int Count { get { return _items.Count; } }
public bool IsSynchronized { get { return _items.IsSynchr onized; } }
public object SyncRoot { get { return _items.SyncRoot ; } }
public Item this[int index] { get { return (Item) _items[index]; } }
public ItemCollection( ) { _items = new ArrayList(); }
public void Add(Item item) { _items.Add(item ); }
public void CopyTo(Array array, int index) { _items.CopyTo(a rray, index); }
public IEnumerator GetEnumerator() { return _items.GetEnume rator(); } }
////////////////////////////////////////////////////////////
Thanks, Kent
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Anthony Bouch |
last post by:
Hi
I've been reading using the XmlSerializer with custom collections.
I've discovered that when serializing a custom collection (a class that
implements ICollection, IList etc.) the XmlSerializer will only serialize
the collection items - with the default root as ArrayofMyItems etc.
My custom collection class has some additional public properties that I
would like to include in the serialization above the items element array (in
|
by: MrNobody |
last post by:
since ArrayList implements ICollection, is there a quick way to convert an
ICollection into ArrayList (besides actually iterating through each element
in the ICollection and explicitly adding them to a new ArrayList instance) ??
|
by: andrew.miadowicz |
last post by:
It's funny that I've only now run into this question, after a few years
of using C#, but I find it intriguiging all the same. All the more so,
that the generic version of ICollection in .Net Framework 2.0 has this
method.
It seems to me that the question whether a collection has some element
should be answerable by any collection, not just IList or IDictionary.
An obvious implementation must exist, since every ICollection inherits...
|
by: Dave Booker |
last post by:
Am I missing something here? It looks like the generic Queue<T> implements
Enumerable<T> and ICollection but not ICollection<T>. (I want to use it in
an interface that wants an ICollection<T>.)
Is there a reason for this, or is it just an oversight in .NET 2.0?
Is there a computationally easy way to cast/convert a Queue<T> to an
ICollection<T>?
|
by: Allan Ebdrup |
last post by:
I have a public sealed Class A that implements the interface I.
Now I want to pass an ICollection<Aas a parameter to a method that takes a
parameter of the type ICollection<Ibut I get the error:
Can't convert from System.Collections.Generic<Ato
System.Collections.Generic<I>
Shouldn't I be able to pass ICollection<Ato a method that takes
ICollection<Iwhen A implements I ?
| |
by: emma_middlebrook |
last post by:
Hi
Hopefully the title is quite accurate but here's some more information.
I have a load of ICollection<references hanging off a class e.g.
ICollection<X>, ICollection<Yetc etc.
Each of the collection's types are all derived from a base class Base
i.e X : Base, Y : Base etc etc.
|
by: cwertman |
last post by:
Ok the XmlSerializer is nice for my need.
BUT I what I need (maybe the XmlSerializer isnt what I need) is to be
able to take the following
objects
A Person who has a collection of Addresses
Person
(fname)
|
by: per9000 |
last post by:
Hi,
*background*
I want a class containing an int (a list of sets of integer). This
should be hidden for the user and he/she should be able to insert
his/her favourite data structure so to be a little fancy I thought I'd
do a Ctor like this:
// this is the "core": private int sets;
|
by: =?Utf-8?B?U2hhd24=?= |
last post by:
Hi;
I would like to be able to use the XMLSerializer to serialize and
deserialize a dictionary. is that possible? i know that you can serialize an
object that implements the ICollection interface. does the fact that
Dictionary inherits from ICollection<cause a problem?
Currently i am using a class that inherits from ICollection and it has a
Dictionary as it's container. but when it gets to creating the serialized
object it blows up. here...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
| |
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |