Paste XML as Serializable Type

Every now and then, there's a really cool feature that's buried somewhere that just hits you and makes you say "Wow, that's insanely helpful, why didn't somebody think of this sooner." 

I was playing around with the BizTalk Services SDK, specifically the different web programming bits and pieces and stumbled up on the Paste As Serializable visual studio add in (navigate to BizTalk Services SDK\Samples\Web\Tooling). 

Let's say you're interested in programming against some services that return normal, plain XML. Well, nobody likes writing code to query XML, Linq to XML is fun, but I'd just really like to mess around with some objects and not really deal with how it looks over the wire or under the hood.

This great little utility lets you select some snippet of XML (say I get it from here, because I am working on a Twitter mashup).  Copy the XML to the clipboard (here's a snippet of what the XML looks like).

    <created_at>Wed May 02 16:54:45 +0000 2007</created_at>
    <text>has advil &amp; beineigts in the back of Steve and Don's talk in 4101B.  Anybody needing either is welcome to some, the pastries will go fast.</text>
      <name>Matt Winkler</name>
      <description>workflow technical evangelist</description>

Go into Visual Studio into a code file and open up the edit menu.


There, clearly indicated by the "this is just an alpha icon," is the option to "Paste XML as Serializable Type".  Here's what it outputs:

   [System.Xml.Serialization.XmlRootAttribute(Namespace = "", ElementName = "statuses")]
    public class StatusesClass

        private System.Collections.Generic.List<StatusClass> statusField;

        [System.Xml.Serialization.XmlElementAttribute(Namespace = "", ElementName = "status")]
        public virtual System.Collections.Generic.List<StatusClass> Status
                return this.statusField;
                this.statusField = value;

        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", ElementName = "status")]
        public class StatusClass

            private string created_atField;
            private string idField;
            private string textField;
            private UserClass userField;
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "", ElementName = "created_at")]
            public virtual string Created_at
                    return this.created_atField;
                    this.created_atField = value;



Remainder of code truncated for the sake of the readers. 

This lets me do some cool stuff like this:

static void Main(string[] args)
    WebHttpClient twc = new WebHttpClient();
    twc.UriTemplate = "";
    StatusesClass sc = twc.Get().GetBody<StatusesClass>();
    Console.WriteLine("Got {0}", sc.Status.Count);

And get my result back in a nice typed object that I can then use elsewhere in my code (or at least debug)



Steve, thanks, this thing rocks!

Comments (3)

  1. Silverlight Toolbar [Via: vivekd ] Paste XML as Serializable Type [Via: mwinkle ] Reflector Addins and…

  2. You’ve been kicked (a good thing) – Trackback from

  3. jessejacob says:

    "Well, nobody likes writing code to query XML"

    I had a good laugh at that. Where I work, nobody likes writing code to query relational databases because everything we do is XML 😉 It’s handy to have this in the interface, esp. for people who don’t like "writing code to query XML", but it’s probably helpful for you people to know you can do this and more from the command line using XSD.EXE.

    Open the Visual Studio Command Prompt and run ‘XSD’. If you have a plain XML file like the example you used above, you can run XSD against that file, and it will infer a schema for you. With that schema, you can then generate classes to represent the entire file just like the biztalk addin, or you can choose to generate classes for individual element subsets. XSD can also generate nice, strongly-typed ADO.NET DataSets if you are so inclined.


Skip to main content