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

How to specify root when serializing collection

How can I change the name of the root element from ArrayOfPasswordEntry to
PasswordList or whatever?
I have tried [XmlRoot("PasswordList")] before the public class
PasswordEntries definition, but XmlSerializer won't accept.

Please help!

Here's the output:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPasswordEntry xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PasswordEntry>
<Name>EntryName</Name>
<Account>AccountName</Account>
<Password>password</Password>
</PasswordEntry>
</ArrayOfPasswordEntry>
Here the code:

using System;

namespace TrayPasswords
{
[Serializable]
public class PasswordEntries : System.Collections.CollectionBase
{
public PasswordEntries()
{
}

public PasswordEntry Add(PasswordEntry value)
{
this.InnerList.Add(value);

return value;
}

public void Remove(int index)
{
PasswordEntry entry = (PasswordEntry) this.InnerList[index];
if (entry != null)
this.InnerList.Remove(entry);
}

/// <summary>An indexer is needed for Serialization of Collection
classes</summary>
[System.Runtime.CompilerServices.IndexerName("Item" )]
public PasswordEntry this[int index]
{
get { return (PasswordEntry) this.InnerList[index]; }
}

/// <summary>An indexer is needed for Serialization of Collection
classes</summary>
[System.Runtime.CompilerServices.IndexerName("Item" )]
public PasswordEntry this[string name]
{
get
{
foreach (PasswordEntry entry in this.InnerList)
{
if (entry.Name == name)
return entry;
}
return null;
}
}
}
}
Here's the output:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPasswordEntry xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PasswordEntry>
<Name>EntryName</Name>
<Account>AccountName</Account>
<Password>password</Password>
</PasswordEntry>
</ArrayOfPasswordEntry>
Nov 11 '05 #1
1 1944
This works for me--

Dim xmlRoot As New XmlRootAttribute("PasswordList")
mySerializer = New XmlSerializer(GetType(PasswordEntries), xmlRoot)

Hope that's helpful.

Jon

"Chris Becker" <sl*****@hotmail.com> wrote in message
news:OS**************@TK2MSFTNGP09.phx.gbl...
How can I change the name of the root element from ArrayOfPasswordEntry to
PasswordList or whatever?
I have tried [XmlRoot("PasswordList")] before the public class
PasswordEntries definition, but XmlSerializer won't accept.

Please help!

Here's the output:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPasswordEntry xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PasswordEntry>
<Name>EntryName</Name>
<Account>AccountName</Account>
<Password>password</Password>
</PasswordEntry>
</ArrayOfPasswordEntry>
Here the code:

using System;

namespace TrayPasswords
{
[Serializable]
public class PasswordEntries : System.Collections.CollectionBase
{
public PasswordEntries()
{
}

public PasswordEntry Add(PasswordEntry value)
{
this.InnerList.Add(value);

return value;
}

public void Remove(int index)
{
PasswordEntry entry = (PasswordEntry) this.InnerList[index];
if (entry != null)
this.InnerList.Remove(entry);
}

/// <summary>An indexer is needed for Serialization of Collection
classes</summary>
[System.Runtime.CompilerServices.IndexerName("Item" )]
public PasswordEntry this[int index]
{
get { return (PasswordEntry) this.InnerList[index]; }
}

/// <summary>An indexer is needed for Serialization of Collection
classes</summary>
[System.Runtime.CompilerServices.IndexerName("Item" )]
public PasswordEntry this[string name]
{
get
{
foreach (PasswordEntry entry in this.InnerList)
{
if (entry.Name == name)
return entry;
}
return null;
}
}
}
}
Here's the output:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPasswordEntry xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PasswordEntry>
<Name>EntryName</Name>
<Account>AccountName</Account>
<Password>password</Password>
</PasswordEntry>
</ArrayOfPasswordEntry>

Nov 11 '05 #2

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

Similar topics

0
by: siddharthkhare | last post by:
HiAll, i am having hard time serializing a object in a specific xml.I need to feed it to a biztalk port. i Have a root object(c# class) 'CTSCServiceOrder'. When i serilize it using a xml...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
1
by: dotNetDave | last post by:
I'm trying to create xml seriaizable collection class (below), but the xml keeps coming out wrong. In the resulting xml from the web service (below) the "ArrayOfAlarmProcessor" tag should really be...
1
by: Carl Gilbert | last post by:
Hi I am trying to serialize a collection of GUIDs by overriding the serialize function on a diagram component I am using. The following code sucessfully serializes a single GUID: Public...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
2
by: Q. John Chen | last post by:
I posted this in C# group and just found this spot. I created a class that implements ICollection. After serializing got following result: <?xml version=\"1.0\" encoding=\"utf-16\">...
3
by: RandomEngineer | last post by:
So here's the challenge... How can a collection (System.Collections.Generic.IList) of some custom type be serialized in a web service using .NET 2.0? Below are the class and the web methods in...
0
by: Keith Patrick | last post by:
Could someone explain to me the ramifications of declaring XmlRoot("namespace") on a series of classes in which one or more are aggregates within another? I have a bunch of classes with a single...
1
by: Karthik1979 | last post by:
I have a custom class inherited from List<T> collection. Along with the base class functionality, I have included my additional properties. When serializing, only the base class items are serialized...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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.