473,811 Members | 3,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1460
I'll give you a solution in VB.NET ;-)
Imports System.Xml.Seri alization
Public Class Root

Private _mainItem As MainItem
'============== =============== =============== =============== ==============
#Region "CONSTRUCTO RS"
'============== =============== =============== =============== ==============
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.Seri alization
Public Class MainItem
Private _alSubItem As New ArrayList
'============== =============== =============== =============== ==============
#Region "CONSTRUCTO RS"
'============== =============== =============== =============== ==============
Public Sub New()
End Sub
#End Region

'============== =============== =============== =============== ==============
#Region "PUBLIC PROPERTY DEFINITIONS"
'============== =============== =============== =============== ==============
<XmlElement(Ele mentName:="SubI tem", Type:=GetType(S ubItem))> _
Public Property alPersonal_clie nt() As ArrayList
Get
Return _alPersonal_cli ent
End Get
Set(ByVal Value As ArrayList)
_alPersonal_cli ent = Value
End Set
End Property
#End Region
'============== =============== =============== =============== ==============
#Region "PUBLIC METHODS"
'============== =============== =============== =============== ==============
Public Sub AddSubItem(ByVa l p_SubItem As SubItem)
_alSubItem.Add( p_SubItem)
End Sub
Public Sub RemoveSubItem(B yVal p_SubItem As SubItem)
_alSubItem.Remo ve(p_SubItem)
End Sub

#End Region

End Class

Imports System.Xml.Seri alization
Public Class SubItem

Private _subItem As String
'============== =============== =============== =============== ===============
#Region "CONSTRUCTO RS"
'============== =============== =============== =============== ===============
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.Seri alization
Public Class Root

Private _mainItem As MainItem
'============== =============== =============== =============== ==============
#Region "CONSTRUCTO RS"

'============== =============== =============== =============== ==============
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.Seri alization
Public Class MainItem
Private _alSubItem As New ArrayList
'============== =============== =============== =============== ==============
#Region "CONSTRUCTO RS"

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

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

'============== =============== =============== =============== ==============
<XmlElement(Ele mentName:="SubI tem", Type:=GetType(S ubItem))> _
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(ByVa l p_SubItem As SubItem)
_alSubItem.Add( p_SubItem)
End Sub
Public Sub RemoveSubItem(B yVal p_SubItem As SubItem)
_alSubItem.Remo ve(p_SubItem)
End Sub

#End Region

End Class

Imports System.Xml.Seri alization
Public Class SubItem

Private _subItem As String
'============== =============== =============== =============== ===============
#Region "CONSTRUCTO RS"

'============== =============== =============== =============== ===============
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("MainI tem"),XmlArrayI tem("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
5034
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
6
2720
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 rmi. Just to keep my question short I am posting trimmed version of my code. //file: Serializable.cs
3
2876
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 able to get a reference to the remote object and that works fine. When I try to make a call to a remote object's method I get an exception: System.Runtime.Serialization.SerializationException: Cannot find the assembly ProcessTest,...
2
2593
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 i had to serialize the objects. While doing that I made the classes serializable whose objects I store in sessions. However, when I run the application I get the following error.
0
2507
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 serializing the object but there seems to be a problem serializing an array of objects. Any help will be appreciated "Cannot assign object of type System.Object to an object of type ElectronicWallet.C2PTest.PaymentItem." :...
2
1635
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 this. So I wrote the following code to serialize\deserialize the object, but now I have the problem that the object has a generic list and when I'm trying to deserialize it, I get an error. <System.Serializable()Public Class TestClass
5
6119
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: Value -8590321990885400808 is outside the valid range . Parameter name: ticks at System.DateTime..ctor (Int64 ticks) at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadPrimitiv
8
12577
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 IO.MemoryStream =====Exception: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
0
6616
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 object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream. XML serialization serializes only the public fields and property values of an object...
0
4715
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 lifetime of those objects lasts only as long as the Java virtual machine is running. Once we close the JVM, we lose all those objects. What if someone wants objects to be available after JVM restarts? This is where Serialization comes into play. ...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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, 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...
0
10384
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 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...
1
10395
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,...
1
7667
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6887
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5553
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...
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.