惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
U
Unit 42
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
B
Blog RSS Feed
The Cloudflare Blog
D
Docker
A
About on SuperTechFans
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
月光博客
月光博客
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
博客园_首页
Stack Overflow Blog
Stack Overflow Blog

博客园 - sharplife

CMMI的五个台阶 记个链接,RMS & Moss 文档保护 2010, Nginx + Apache, mod_wsgi serve python web application (test with TG2) CSSHttpRequest : cross domain ajax request for easy Pylons unicode document Python 2.x Unicode 简记 TurboGears 2.0 beta6 安装 严冬 感受危机 这就是生活了 汉字及英文字符的ascii表示 推荐阅读:如何读一本书 [转]做人做事~ 十一结束~ 离开 css image crop tech c# 3.0 quick reference XML Entities 参考
[转]Create an XMLSerializer from a given XML string
sharplife · 2008-10-08 · via 博客园 - sharplife

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 the XMLSerializer class, allowing you to construct or serialize an object of a 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 a 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 a stream, which is needed to intialize the XMLSerializer

By Christian Weyer

[...]
string xmlString = @"<?xml version='1.0' encoding='utf-8' ?><TestType><Number>42</Number><Value>Christian</Value></TestType>";

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
{
public int Number;
public string Value;
}