Title: XML Support in Visual Basic 9.0
1XML Support in Visual Basic 9.0
2A Simple Exercise
ltpurchaseOrder orderDate"1999-10-20"gt ltshipTo
country"US"gt ltnamegtAlice Smithlt/namegt
ltstreetgt123 Maple Streetlt/streetgt ltcitygtMill
Valleylt/citygt ltstategtCAlt/stategt
ltzipgt90952lt/zipgt lt/shipTogt ltbillTo
country"US"gt ltnamegtRobert Smithlt/namegt
ltstreetgt8 Oak Avenuelt/streetgt ltcitygtOld
Townlt/citygt ltstategtPAlt/stategt
ltzipgt95819lt/zipgt lt/billTogt ltitemsgt ltitem
partNum"872-AA"gt ltproductNamegtLawnmowerlt/pr
oductNamegt ltquantitygt1lt/quantitygt
ltpricegt148.95lt/pricegt lt/itemgt ltitem
partNum"926-AA"gt ltproductNamegtBaby
Monitorlt/productNamegt ltquantitygt1lt/quantitygt
ltpricegt39.98lt/pricegt
ltshipDategt1999-05-21lt/shipDategt lt/itemgt
lt/itemsgt lt/purchaseOrdergt
- Create the document
- Print it!
- Compute total price
- Add new child
3XLINQ Key Design Points
- Smaller, Faster
- Document Free
- Functional Construction
- Uniform Axis
- Easy to Extract Values
- XML Name Simplification
4Object Model
XName
XAttribute
XNode
XCData
XComment
XContainer
XDeclaration
XDocumentType
XProcessing Instruction
XDocument
XElement
No Text Node
5XML In VB 9
Better, but not yet good enough
Function Total(PO As XElement) As Double For
Each Dim Item In PO.Descendants("item") Dim
Price As Double Item.Element("price") Dim
Quantity As Integer Item.Element("quantity")
Total QuantityPrice Next End Function
PO.Add(New XElement("Total", _ New
XAttribute("Price", Total(PO))))
Too many strings, interpretative code.
Does not look like XML
6XML Literals
Dim PO _ltpurchaseOrder orderDate"1999-10-20"gt
ltshipTo country"US"gt ltnamegtAlice
Smithlt/namegt ltstreetgt123 Maple
Streetlt/streetgt ltcitygtMill Valleylt/citygt
ltstategtCAlt/stategt ltzipgt90952lt/zipgt
lt/shipTogt ltbillTo country"US"gt
ltnamegtRobert Smithlt/namegt ltstreetgt8 Oak
Avenuelt/streetgt ltcitygtOld Townlt/citygt
ltstategtPAlt/stategt ltzipgt95819lt/zipgt
lt/billTogt ltitemsgt ltitem partNum"872-AA"gt
ltproductNamegtLawnmowerlt/productNamegt
ltquantitygt1lt/quantitygt ltpricegt148.95lt/pricegt
lt/itemgt ltitem partNum"926-AA"gt
ltproductNamegtBaby Monitorlt/productNamegt
ltquantitygt1lt/quantitygt ltpricegt39.98lt/pricegt
ltshipDategt1999-05-21lt/shipDategt
lt/itemgt lt/itemsgt lt/purchaseOrdergt
CutPaste XML
7XML Literals
Dim ShipTo _ ltshipTo country"US"gt
ltnamegtAlice Smithlt/namegt ltstreetgt123 Maple
Streetlt/streetgt ltcitygtMill Valleylt/citygt
ltstategtCAlt/stategt ltzipgt90952lt/zipgt
lt/shipTogtDim ShipTo ltbillTo
country"US"gtlt/billTogt Dim ShipTo
ltitemsgtlt/itemsgt Dim PO _
ltpurchaseOrder orderDatelt System.DateTime.Today
gtgt lt ShipTo gtlt BillTo gtlt
Items gt lt/gt
CutPaste XML
ASP.Net style holes (anywhere XLINQ allows
expression)
Compiled to imperative construction!
Short end-tags
Offside layout rule
8Easier Namespaces
Dim ShipTo _ ltshipTo xmlns""
country"US"gt ltnamegtAlice Smithlt/namegt
ltstreetgt123 Maple Streetlt/streetgt
ltprefixcity xmlnsprefix""gt Mill
Valley lt/prefixcitygt ltstategtCAlt/stategt
ltzipgt90952lt/zipgt lt/shipTogt
9Axis Members
x As XElement/IEnumerable(Of XElement)x.m ?
x.Elements("m")x._at_m ? x.Attribute("m")x...m ?
x.Descendants("m")
Function Total(PO As XElement) As Double For
Each Dim Item In PO...item Dim Price As
Double Item.price Dim Quantity As Integer
Item.quantity Total QuantityPrice
Next End Function Function Total(PO As XElement)
As Double Return Select Sum(QuantityPrice)
From PO...item End Function
10Conclusions
- New modern, redesigned XML API
- Easier, lighter DOM
- XQuery expressiveness
- Cross data-model queries via LINQ
- XML is first class citizen in VB9
http//msdn.microsoft.com/netframework/future/linq
/http//msdn.microsoft.com/vbasic/future/default.
aspx