.NETGURU
Serialize an object into XML
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngxml' list.


Karell Ste-Marie
Hello Folks,

I currently am at a loss on how to perform some seemingly simple object
serialization, for those that have an in-dept knowledge about the
subject I'm attempting "shallow" serialization.

I currently am able to serialize an object using this code:

Imports System.Xml
Imports System.IO
Imports System.Xml.Serialization

Public Class dummyItem
Public itemCode As String
Public itemDescription As String
End Class

Public Class pointsItemSerializable
Public item As pointsItem

Public Sub New()
item =3D New pointsItem()
End Sub

Public Function Serialize() As String
Dim x As XmlSerializer =3D New =
XmlSerializer(GetType(pointsItem))
Dim m As MemoryStream =3D New MemoryStream()
Dim functionResult As String
Dim b As Byte()
x.Serialize(m, item)
m.Position =3D 0
b =3D m.ToArray()
Dim z As Integer
functionResult =3D ""
For z =3D 1 To b.Length
functionResult =3D functionResult & Chr(b(z - 1))
Next
Return functionResult
End Function
End Class

The portion that finds a way to get to me is the deserialization of an
object, I want to avoid using a file (even a temporary one) because
these XML objects are going to be stored in a database as TEXT
datatypes. What I would really want to be able to do (if such a thing
was possible) was to give something a STRING type and get my object type
in return.

________________________
Karell Ste-Marie
C.I.O. BrainBank Inc
475 Dumont, Suite 202
Dorval, Quebec, H9S 5W2, Canada
Tel: (514) 636-6655
Fax: (514) 636-9824

Reply to this message...
 
    
John Howes
Try this (the key is first getting your string into a stream, and then
deserializing the stream data):=20

        public static object LoadObjectFromXml(System.Type type,
string xml)
        {
            if (null !=3D xml && xml.Length > 0)
            {
                MemoryStream ms =3D new MemoryStream();
                StreamWriter sw =3D new StreamWriter(ms);

                try
                {
                    sw.AutoFlush =3D true;
                    sw.Write(xml);
                    return LoadPersistedObject(type,
ms);
                }
                catch (Exception exc)
                {
                    throw new
ApplicationException("The XML could not be loaded.", exc);
                }
                finally
                {
                    sw.Close();
                    ms.Close();
                }
            }
            return null;
        }

        public static object LoadPersistedObject(System.Type
type, Stream stream)
        {
            XmlSerializer s =3D new XmlSerializer(type);

            try
            {
                stream.Position =3D 0;
                object o =3D s.Deserialize(stream);
                return o;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

-----Original Message-----
From: Karell Ste-Marie [mailto:Click here to reveal e-mail address]=20
Sent: Monday, July 22, 2002 1:19 PM
To: aspngxml
Subject: [aspngxml] Serialize an object into XML

Hello Folks,

I currently am at a loss on how to perform some seemingly simple object
serialization, for those that have an in-dept knowledge about the
subject I'm attempting "shallow" serialization.

I currently am able to serialize an object using this code:

Imports System.Xml
Imports System.IO
Imports System.Xml.Serialization

Public Class dummyItem
Public itemCode As String
Public itemDescription As String
End Class

Public Class pointsItemSerializable
Public item As pointsItem

Public Sub New()
item =3D New pointsItem()
End Sub

Public Function Serialize() As String
Dim x As XmlSerializer =3D New =
XmlSerializer(GetType(pointsItem))
Dim m As MemoryStream =3D New MemoryStream()
Dim functionResult As String
Dim b As Byte()
x.Serialize(m, item)
m.Position =3D 0
b =3D m.ToArray()
Dim z As Integer
functionResult =3D ""
For z =3D 1 To b.Length
functionResult =3D functionResult & Chr(b(z - 1))
Next
Return functionResult
End Function
End Class

The portion that finds a way to get to me is the deserialization of an
object, I want to avoid using a file (even a temporary one) because
these XML objects are going to be stored in a database as TEXT
datatypes. What I would really want to be able to do (if such a thing
was possible) was to give something a STRING type and get my object type
in return.

________________________
Karell Ste-Marie
C.I.O. BrainBank Inc
475 Dumont, Suite 202
Dorval, Quebec, H9S 5W2, Canada
Tel: (514) 636-6655
Fax: (514) 636-9824

| [aspngxml] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngxml.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
 
System.ApplicationException
System.IO.MemoryStream
System.IO.StreamWriter
System.Type
System.Xml.Serialization.XmlSerializer




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification