[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vb.net - XML - Serialize to string and not to file/stream

Posted on 2003-10-27
6
Medium Priority
?
534 Views
Last Modified: 2007-12-19
Hi
Please tell me how I can serialize to a string, not to a file.

For example:
Dim serializer as New XmlSerializser(something.GetType())
Dim result as string
serializer.serialize(result, something.GetType())


See how I want the result to be put into a string, not written to a stream.

Thanks
Daniel
0
Comment
Question by:jasww
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
bacon7181 earned 375 total points
ID: 9626525
               Dim Serializer As New System.Xml.Serialization.XmlSerializer(something.GetType())
                ' We will serialize to a Memory Stream
                Dim ms As New MemoryStream
                Serializer.Serialize(ms, something)
                ' Then convert the memory stream to a string
                Dim myString As String = Convert.ToBase64String(ms.GetBuffer())
0
 

Author Comment

by:jasww
ID: 9626564
Thanks - I just need the IMPORTS "foo.bar.memory"  part for MemoryStream object please...
0
 
LVL 3

Expert Comment

by:bacon7181
ID: 9626586
System.IO
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 

Author Comment

by:jasww
ID: 9626796
OK,
all I am getting is base64 garbage.
I need the original string.
convert.ToString() does not work.

Thanks
Daniel
0
 

Author Comment

by:jasww
ID: 9626818
It's ok


System.Text.Encoding.UTF8.GetString(ms.getBuffer())
0
 
LVL 10

Expert Comment

by:GoodJun
ID: 9626823
dim buffer as new MemoryStream()
dim formatter as New XmlSeriallizer(yourObject.GetType)

formatter.Serialize(buffer, yourObject)

dim output as string
output=system.text.encoding.ascii.getstring(buffer.ToArray)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question