When Visual Basic first came out, no one had even heard of XML. But now it's everywhere. It's like that black oozing stuff that bonds with Peter Parker in Spiderman 3, but without all the creepy special effects. And now in Visual Basic 2008, XML is part of the language syntax itself. When will it end?
It turns out that making XML part of the language is pretty cool. In the old-way section a few pages ago, I showed you some code that created the XML product list for "Chai." The XML content was 11 lines long, but it took nearly 50 lines of source code to produce it. But you can build that same XML content using the new way in pretty close to the final 11 lines.
Dim chaiItem As System.Xml.Linq.XDocument = _ <?xml version="1.0"?> <productList> <!-- We currently sell these items. --> <supplier ID="652" fullName="Beverages R Us"> <product ID="1" available="Yes"> <productName>Chai</productName> <category>Beverages</category> <unitPrice>18.00</unitPrice> </product> </supplier> </productList>
How about that! Except for the first declaration line, the content is identical to the final XML. The new XML Literals feature makes building XML documents a snap. The content gets stored in the new
XDocument object, part of the
System.Xml.Linq namespace. If you want to store just a section of XML instead of the entire document, use the
XElement class instead.
Dim productSection As System.Xml.Linq.XElement = _ <product ID="1" available="Yes"> <productName>Chai</productName> ...