473,405 Members | 2,344 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,405 software developers and data experts.

XML Serialization of Object

how would i structure a C# class to properly serialize into the
following XML Schema

<root>
<MainItem>
<SubItem/>
<SubItem/>
</MainItem>
</root>

If i create a property as an array of SubItem objects, then it will add
a parent element name "SubItems" plural, which i don't want.

thanks

Mar 5 '06 #1
4 1446
I'll give you a solution in VB.NET ;-)
Imports System.Xml.Serialization
Public Class Root

Private _mainItem As MainItem
'================================================= ========================
#Region "CONSTRUCTORS"
'================================================= ========================
Public Sub New()
End Sub
#End Region

'================================================= ========================
#Region "PUBLIC PROPERTY DEFINITIONS"
'================================================= ========================
Public Property MainItem() As MainItem
Get
Return _mainItem
End Get
Set(ByVal Value As MainItem)
_mainItem = Value
End Set
End Property
#End Region
End Class

Imports System.Xml.Serialization
Public Class MainItem
Private _alSubItem As New ArrayList
'================================================= ========================
#Region "CONSTRUCTORS"
'================================================= ========================
Public Sub New()
End Sub
#End Region

'================================================= ========================
#Region "PUBLIC PROPERTY DEFINITIONS"
'================================================= ========================
<XmlElement(ElementName:="SubItem", Type:=GetType(SubItem))> _
Public Property alPersonal_client() As ArrayList
Get
Return _alPersonal_client
End Get
Set(ByVal Value As ArrayList)
_alPersonal_client = Value
End Set
End Property
#End Region
'================================================= ========================
#Region "PUBLIC METHODS"
'================================================= ========================
Public Sub AddSubItem(ByVal p_SubItem As SubItem)
_alSubItem.Add(p_SubItem)
End Sub
Public Sub RemoveSubItem(ByVal p_SubItem As SubItem)
_alSubItem.Remove(p_SubItem)
End Sub

#End Region

End Class

Imports System.Xml.Serialization
Public Class SubItem

Private _subItem As String
'================================================= =========================
#Region "CONSTRUCTORS"
'================================================= =========================
Public Sub New()
End Sub
#End Region

'================================================= ==========================
#Region "PUBLIC PROPERTY DEFINITIONS"
'================================================= ==========================
<XmlText> _
Public Property Text() As String
Get
Return _subItem
End Get
Set(ByVal Value As String)
_subItem = Value
End Set
End Property
#End Region
End Class
'================================================= ==================================
' END OF FILE
'================================================= ==================================

Mar 6 '06 #2
Imports System.Xml.Serialization
Public Class Root

Private _mainItem As MainItem
'================================================= ========================
#Region "CONSTRUCTORS"

'================================================= ========================
Public Sub New()
End Sub
#End Region

'================================================= ========================
#Region "PUBLIC PROPERTY DEFINITIONS"

'================================================= ========================
Public Property MainItem() As MainItem
Get
Return _mainItem
End Get
Set(ByVal Value As MainItem)
_mainItem = Value
End Set
End Property
#End Region
End Class

Imports System.Xml.Serialization
Public Class MainItem
Private _alSubItem As New ArrayList
'================================================= ========================
#Region "CONSTRUCTORS"

'================================================= ========================
Public Sub New()
End Sub
#End Region

'================================================= ========================
#Region "PUBLIC PROPERTY DEFINITIONS"

'================================================= ========================
<XmlElement(ElementName:="SubItem", Type:=GetType(SubItem))> _
Public Property alSubItem() As ArrayList
Get
Return _alSubItem
End Get
Set(ByVal Value As ArrayList)
_alSubItem = Value
End Set
End Property
#End Region
'================================================= ========================
#Region "PUBLIC METHODS"

'================================================= ========================
Public Sub AddSubItem(ByVal p_SubItem As SubItem)
_alSubItem.Add(p_SubItem)
End Sub
Public Sub RemoveSubItem(ByVal p_SubItem As SubItem)
_alSubItem.Remove(p_SubItem)
End Sub

#End Region

End Class

Imports System.Xml.Serialization
Public Class SubItem

Private _subItem As String
'================================================= =========================
#Region "CONSTRUCTORS"

'================================================= =========================
Public Sub New()
End Sub
#End Region

'================================================= ==========================
#Region "PUBLIC PROPERTY DEFINITIONS"

'================================================= ==========================
<XmlText> _
Public Property Text() As String
Get
Return _subItem
End Get
Set(ByVal Value As String)
_subItem = Value
End Set
End Property
#End Region
End Class
'================================================= ==================================
' END OF FILE
'================================================= ==================================

Mar 6 '06 #3
Sorry that was VB.NET - You'll be able to pick out the relevant bits

Dickster

Mar 6 '06 #4
try this

[XmlRoot("root")]
public class MyClass
{
[XmlArray("MainItem"),XmlArrayItem("SubItem")]
public string[] Items;
}

"jw*****@gmail.com" wrote:
how would i structure a C# class to properly serialize into the
following XML Schema

<root>
<MainItem>
<SubItem/>
<SubItem/>
</MainItem>
</root>

If i create a property as an array of SubItem objects, then it will add
a parent element name "SubItems" plural, which i don't want.

thanks

Mar 6 '06 #5

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Steve | last post by:
I've been following a couple remoting tutorials on the web, they are all pretty much the same. I've got my different applications built(client, server and remote object (dll)) The client is...
2
by: Maximus | last post by:
Hi Everyone, I was using Inprocess session objects, but incase of aspnet process crashes the session objects were lost as a result I decided to shift to out of porocess session objects. For this...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
2
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
8
by: =?Utf-8?B?UGlnZ3k=?= | last post by:
Hi to all, I am getting this System.OutOfMemoryException calling the Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream>,<Obj>) method. The type of <streamis...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
0
by: JosAH | last post by:
Introduction Upon hearing the word, "Serialization", the first question which comes to mind is ... "What is Serialization?" We know that we can create resusable objects in Java. But the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.