---
title: "HOWTO: Create an XMLSerializer from a given XML string"
url: "https://osherove.com/blog/2003/3/26/howto-create-an-xmlserializer-from-a-given-xml-string.md"
canonical: "https://osherove.com/blog/2003/3/26/howto-create-an-xmlserializer-from-a-given-xml-string.html"
published: "2003-03-26T07:32:00"
updated: "2003-03-26T07:32:00"
tags: []
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# HOWTO: Create an XMLSerializer from a given XML string

By Roy Osherove March 26, 2003

**Historical article** · Originally published March 26, 2003; technical details may be outdated.

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**

<table width="100%"><tbody><tr><td>By : <a href="http://dotnetweblogs.com/Jan" id="_ctl0__ctl1_CommentList__ctl0_NameLink">Jan Tielens</a></td></tr><tr><td></td></tr><tr><td><span id="_ctl0__ctl1_CommentList__ctl0_PostText"><table width="100%"><tbody><tr><td></td></tr><tr><td></td></tr><tr><td><span id="_ctl0__ctl1_CommentList__ctl0_PostText">You can use the StringBuilder to create a stream.<br><br>I use it with CodeDom too, like this:<br>Private Function GetVBCode(ByVal code As CodeTypeDeclaration) As String<br>Dim gen As New VBCodeProvider<br><br>Dim sb As New System.Text.StringBuilder<br>Dim stream As New IO.StringWriter(sb)<br>gen.CreateGenerator().GenerateCodeFromType(code, stream, Nothing)<br>stream.Close()<br><br>Return sb.ToString<br>End Function<br></span></td></tr></tbody></table><br><br></span></td></tr></tbody></table>

Solution 2:  Use a MemoryStream Object to translate the string into a stream, which is needed to intialize the XMLSerializer

**

<table width="100%"><tbody><tr><td>By&nbsp;<a href="http://dotnetweblogs.com/Cweyer" id="_ctl0__ctl1_CommentList__ctl2_NameLink">Christian Weyer</a></td></tr><tr><td></td></tr><tr><td><span id="_ctl0__ctl1_CommentList__ctl2_PostText"><p><br><br>[...]<br>string xmlString = @"&lt;?xml version='1.0' encoding='utf-8' ?&gt;&lt;TestType&gt;&lt;Number&gt;42&lt;/Number&gt;&lt;Value&gt;Christian&lt;/Value&gt;&lt;/TestType&gt;";<br><br>System.IO.MemoryStream ms = new System.IO.MemoryStream();<br>System.IO.TextWriter writer = new System.IO.StreamWriter( ms );<br>writer.Write(xmlString);<br>writer.Flush();<br><br>ms.Seek(0, System.IO.SeekOrigin.Begin);<br>XmlSerializer serializer = new XmlSerializer(typeof(TestType));<br>TestType tt = (TestType)serializer.Deserialize(ms);<br>[...]<br><br>public class TestType<br>{<br>public int Number;<br>public string Value;<br>}<br></p></span></td></tr></tbody></table>


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
