Groups | Blog | Home
all groups > dotnet xml > march 2006 >

dotnet xml : XML Serialization of Object


jw56578 NO[at]SPAM gmail.com
3/4/2006 3:56:53 PM
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
dickster
3/6/2006 8:06:28 AM
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
'===================================================================================
dickster
3/6/2006 8:14:08 AM
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
'===================================================================================
dickster
3/6/2006 8:15:43 AM
Sorry that was VB.NET - You'll be able to pick out the relevant bits

Dickster
FoxtrotEcho
3/6/2006 3:28:27 PM
try this

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

[quoted text, click to view]
AddThis Social Bookmark Button