Paste XML as Serializable Type

Paste XML as Serializable Type

  • Comments 3

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).

<statuses>
  <status>
    <created_at>Wed May 02 16:54:45 +0000 2007</created_at>
    <id>47434042</id>
    <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>
    <user>
      <id>5440022</id>
      <name>Matt Winkler</name>
      <screen_name>mwinkle</screen_name>
      <location>Seattle</location>
      <description>workflow technical evangelist</description>
      <profile_image_url>http://assets2.twitter.com/system/user/profile_image/5440022/normal/Matt_Winkler-72dpi.jpg?1177350091</profile_image_url>
      <url>http://blogs.msdn.com/mwinkle</url>
      <protected>false</protected>
    </user>
  </status>

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

image

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
        {
            get
            {
                return this.statusField;
            }
            set
            {
                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
            {
                get
                {
                    return this.created_atField;
                }
                set
                {
                    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 = "http://twitter.com/statuses/user_timeline/mwinkle.xml";
    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)

 

image

Steve, thanks, this thing rocks!

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

  • You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • "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.

    JJ

Page 1 of 1 (3 items)