Create an xmlserializer from a given XML string

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.