다음을 통해 공유


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

 <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>https://assets2.twitter.com/system/user/profile_image/5440022/normal/Matt_Winkler-72dpi.jpg?1177350091</profile_image_url>
      <url>https://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 = "https://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!

Comments

  • Anonymous
    May 17, 2007
    Silverlight Toolbar [Via: vivekd ] Paste XML as Serializable Type [Via: mwinkle ] Reflector Addins and...

  • Anonymous
    August 23, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    August 24, 2007
    The comment has been removed