Howto: Create an xmlserializer from a given XML string
Well, I got some great replies on my appearently rookie question...;). I'll
Post them here for others to learn from. That's what it's all about, isn' t
It?
The Problem: Given an XML string, create an instance of
Xmlserializer class, allowing you to construct or serialize an object of
Specific type.
There were two solutions right away. They both seem pretty darn obvious-
Once you read them Thanks folks!
Solution 1: Use a stringbuilder object to create
Stream
by: Jan tielens |
|
|
|
You can use the stringbuilder to create a stream. I use it with codedom too, like this: private function getvbcode (byval code as codetypedeclaration) as string dim Gen as new vbcodeprovider dim Sb as new system. text. stringbuilder dim stream as New Io. stringwriter (SB) Gen. creategenerator (). generatecodefromtype (Code, stream, nothing) stream. close () return sb. tostring end function |
|
Solution 2: Use a memorystream object to translate the string into
Stream, which is needed to intialize the xmlserializer
by Christian weyer |
|
[...] string xmlstring = @ " encoding = 'utf-8' ?> 42 Christian "; system. io. memorystream MS = new system. io. memorystream (); system. io. textwriter writer = new system. io. streamwriter (MS); writer. write (xmlstring); writer. flush (); MS. seek (0, system. io. seekorigin. begin); xmlserializer serializer = new xmlserializer (typeof (testtype); testtype TT = (testtype) serializer. deserialize (MS); [...] public class testtype {< br> Public int number; Public string value; } |