Live from Redmond: WebCast Conquering XML with Visual Basic 9 Amanda Silver Lead Program Manager Visual Basic Level: Starts Easy, Finishes Advanced
Visual Basic 9 Goals Simplify querying data –Integrate query and transform operations –Unify query of object, relational, and XML data Simplify working with XML –Impose structure on XML w/no schema –Produce XML documents quickly –Access XML members easily
Conquering XML (Agenda) Visual Basic 9 & the XML Data Type –XML Literals –XML Properties Explanation of the LINQ to XML API –Mapping the XML Data Type to the LINQ to XML API Using the XML data type in queries Writing an XML transform in VB Miscellaneous tips and tricks …Be the most productive when programming against XML
Language INtegrated Query (LINQ) LINQ enabled data sources LINQ To Objects Objects LINQ To XML XML LINQ enabled ADO.NET LINQ To Datasets LINQ To SQL LINQ To Entities Relational Others… VB C#.NET Language-Integrated Query
DEMO
Programming XML Today Dim doc As New XmlDocument() Dim contacts As XMLElement = doc.CreateElement("contacts") For Each cust in Customers If (cust.Country = "USA") Dim e As XMLElement = doc.CreateElement("contact") Dim name As XMLElement = doc.CreateElement("name") name.InnerText = c.CompanyName e.AppendChild(name) Dim phone As XMLElement = doc.CreateElement("phone") phone.InnerText = cust.Phone e.AppendChild(phone) contacts.AppendChild(e) End If Next doc.AppendChild(contacts) <contacts> Great Lakes Food Great Lakes Food (503) (503) …</contacts> Imperative model Document- centric No integrated queries Memory- intensive
LINQ to XML Dim contacts As New XElement("contacts", _ From cust in customers _ Where cust.Country = "USA“ _ Select New XElement("contact", _ New XElement("name", cust.CompanyName), _ New XElement("phone", cust.Phone) _ ) Declarative model Element- centric Integrated queries Smaller and faster
LINQ to XML Language integrated query for XML –Expressive power of XPath/Xquery –But with Visual Basic as your programming language! –No conceptual barrier between XML and code Leverages experience with DOM –Element-centric, not document-centric –Symmetry in element/attribute APIs –Functional construction –Text nodes are just strings –Simplified XML namespace support –Faster and smaller than DOM
DEMO
Integrated XML in Visual Basic Dim contacts = _ <%= _ From cust In customers _ Where cust.Country = "USA" _ Select _ %> Infers Xml.Linq XElementInfers Xml.Linq XElement No conceptual barrier WYSIWYG! Expression holes for computed values
XML Literals Shorthand for object creation Dim emp = _ Joe 28 Engineering Dim emp = _ New XElement("employee", _ New XElement("name", "Joe"), _ New XElement("age", 28), _ New XElement("department", _ New XElement("name", "Engineering"), _ New XAttribute("id", 432)))
DEMO
Element access covers all XML axes Dim employees As XElement = GetCurrentEmployeesByDept(“IT”) Dim deptID As Integer = employees.Attribute(“DeptID") Dim emp As XElement = employees.Descendents(“Employee")(0) Dim empDOB As Date = emp.Element(“DateOfBirth“).Value XML Element Access Dim employees As XElement = GetCurrentEmployeesByDept(“IT”) Dim deptID As Integer = Dim emp As XElement = employees… (0) Dim empDOB As Date = emp..Value Attributes Descendents Elements Nancy Davolio Nancy Davolio Sales Representative Sales Representative Andrew Fuller Andrew Fuller Vice President, Sales Vice President, Sales </Employees>
DEMO
Call to action Download Beta1! Join discussion on MSDN LINQ forum Blogosphere/Community: –VBDevCenter: –VB Team: –Paul Vick:
©2006 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.