« XLINQ to XML support in VB9 | Main | Mixing XmlSerializers with XElements and... »

In the May 2006 CTP of Visual Studio there was a great "Paste XML as XElement" feature that didn't make it into the product. However, seems features can be snuck in included as Samples.

In VB9, you can do really cool things like this with XML literals:

 Dim books = <bookstore xmlns="http://examples.books.com">
                        <book publicationdate=<%= publicationdate %> ISBN=<%= isbn %>>
                            <title>ASP.NET Book</title>
                            <price><%= price %></price>
                            <author>
                                <first-name><%= a.FirstName %></first-name>
                                <last-name><%= a.LastName %></last-name>
                            </author>
                        </book>
                    </bookstore>

In C# it's more of a hassle, because you have to build the XElement tree yourself:

  XNamespace ns = "http://example.books.com";
        XDocument books = new XDocument(
            new XElement(ns + "bookstore",
                new XElement(ns + "book",
                    new XAttribute("publicationdate", publicationdate),
                    new XAttribute("ISBN", isbn),
                    new XElement(ns + "title", "ASP.NET Book"),
                    new XElement(ns + "price", price),
                    new XElement(ns + "author", 
                        new XElement(ns + "first-name", a.FirstName),
                        new XElement(ns + "last-name", a.LastName)
                        )
                    )
               )
            );

However, the Paste XML as XLinq/XElement Addin Sample in Visual Studio 2008 adds a new menu item to the Edit Menu.

PasteXmlAsXLinq

From Visual Studio 2008 Beta 2, select Help, then Samples. Click on Visual C# Samples, Linq Samples, then PasteXmlAsXLinq. At this point, you're actually inside a Zip file, so you might want to backup before you drag them out, or just unzip the whole "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip" to a development directory. 

imageDrop out to a Visual Studio 2008 command prompt and run MSBuild PasteXmlAsLinq.csproj (or compile it inside of VS). Take the XmlToXLinq.dll and XmlToXLinq.AddIn and put them in one of the addin folders. Usually this is in something like c:\Users\Scott\Documents\Visual Studio 2008, but I like my things tidy, so I made a separate folder in my Dev Folder just for AddIns. From VS, to Tools|Options|Environment|AddIns and add a new folder if you like and copy those two files in there.

Restart VS and you'll get this new option. It'll take whatever XML you have in the clipboard and paste it as an XElement declaration.

It's not XML Literals for C#, but it does make life easier.



Tuesday, August 28, 2007 10:49:00 AM (Pacific Standard Time, UTC-08:00)
Hey cool! Didn't know you could do that. Long live VB!!
Tuesday, August 28, 2007 11:04:24 AM (Pacific Standard Time, UTC-08:00)
Even though C# is the most hyped and probably used language in the .NET framework, VB still has some small things that we still can't do in C#. Long live add-ins to make things happen.
Wednesday, August 29, 2007 3:38:42 AM (Pacific Standard Time, UTC-08:00)
long live vb too!
chakrit
Comments are closed.

Contact

Sponsors

Hosting By

Hot Topics

Tags

Calendar

<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

November, 2009 (2)
October, 2009 (19)
September, 2009 (11)
August, 2009 (12)
July, 2009 (21)
June, 2009 (26)
May, 2009 (16)
April, 2009 (13)
March, 2009 (17)
February, 2009 (17)
January, 2009 (18)
December, 2008 (32)
November, 2008 (17)
October, 2008 (22)
September, 2008 (16)
August, 2008 (14)
July, 2008 (25)
June, 2008 (19)
May, 2008 (17)
April, 2008 (17)
March, 2008 (26)
February, 2008 (21)
January, 2008 (28)
December, 2007 (19)
November, 2007 (17)
October, 2007 (31)
September, 2007 (39)
August, 2007 (37)
July, 2007 (43)
June, 2007 (37)
May, 2007 (32)
April, 2007 (38)
March, 2007 (29)
February, 2007 (46)
January, 2007 (31)
December, 2006 (27)
November, 2006 (31)
October, 2006 (32)
September, 2006 (39)
August, 2006 (34)
July, 2006 (40)
June, 2006 (18)
May, 2006 (31)
April, 2006 (34)
March, 2006 (30)
February, 2006 (38)
January, 2006 (44)
December, 2005 (19)
November, 2005 (34)
October, 2005 (24)
September, 2005 (37)
August, 2005 (20)
July, 2005 (24)
June, 2005 (33)
May, 2005 (16)
April, 2005 (22)
March, 2005 (34)
February, 2005 (15)
January, 2005 (37)
December, 2004 (28)
November, 2004 (30)
October, 2004 (34)
September, 2004 (22)
August, 2004 (34)
July, 2004 (18)
June, 2004 (64)
May, 2004 (49)
April, 2004 (21)
March, 2004 (29)
February, 2004 (29)
January, 2004 (36)
December, 2003 (25)
November, 2003 (24)
October, 2003 (59)
September, 2003 (42)
August, 2003 (24)
July, 2003 (44)
June, 2003 (29)
May, 2003 (21)
April, 2003 (30)
March, 2003 (27)
February, 2003 (47)
January, 2003 (50)
December, 2002 (31)
November, 2002 (38)
October, 2002 (44)
September, 2002 (15)
May, 2002 (2)
April, 2002 (4)

Google Ads