Tuesday, May 13, 2008

XML LINQing in the Real World

with Paul D. Sheriff

What is LINQ to XML

  • LINQ to XML was developed with Language-Integrated Query over XML in mind and takes advantage of standard query operators and adds query extensions specific to XML
  • Found in System. XML. LINQ NameSpace and adds
    • XDocument type
    • XElement type
  • If you want to use XPath elements you need to add the NameSpace as the are extension methods
  • LINQ provides compile time exceptions where you’d rely on runtime exceptions before

VB.Net versus C#

  • VB.Net does a better job on the LINQ to XML stuff
  • VB.Net has XML literals
    • as long as you have an XSD file with your xml file, you'll get intellisense

Uses

  • useful for Rapid Application Development and prototyping
  • You can do CRUD of XML with LINQ too
  • Allows for easy creation of XML from scratch in code
    • use this for unit testing perhaps
  • LINQ allows you to aggregate data as well
    • VB.Net has an Aggregate/Into syntax as well. Probably not the best approach.
  • You can insert script blocks into inline SQL to create xml data that contains dynamic data <%=something%>
    • This is useful for creating xml documents from data in a database

Anonymous types

  • These are on the fly classes
  • Scoped within the block of code in which it is defined
  • You can return anonymous types, but you get no intellisense
  • These do not work with updates

EFPRS

  • The translat can be rewritten (again) to use this approach to make it more maintainable

Notes

No comments: