extending system xml n.
Skip this Video
Download Presentation
Extending System.Xml

Loading in 2 Seconds...

play fullscreen
1 / 25

Extending System.Xml - PowerPoint PPT Presentation

  • Uploaded on

Extending System.Xml. Ted Neward http://www.tedneward.com. XML in .NET. .NET has tremendous XML support “out of the box” Both Infoset-based and stream-based parser support XPath navigation across Infosets Schema-to-object support XML Serialization … and so on. XML in .NET.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Extending System.Xml' - aviv

Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
extending system xml

Extending System.Xml

Ted Neward


xml in net
  • .NET has tremendous XML support “out of the box”
    • Both Infoset-based and stream-based parser support
    • XPath navigation across Infosets
    • Schema-to-object support
    • XML Serialization
    • … and so on
xml in net1
  • Despite this, sometimes it’s just not enough
    • Working with XML in a strongly-typed language is awkward
    • XSLT doesn’t have all the behavior we want
    • We need to “reach out” from an XSLT to someplace else (database?)
    • We want to “extend” XPath to other hierarchical storage (Registry? filesystem? database? XML database?)
  • Fortunately, System.Xml provides for this
extending system xml1
Extending System.Xml
  • The .NET XML classes are highly extensible
    • We’ll look at four ways to extend the XML support in .NET
    • Extend XmlDocument and friends
    • Extend XmlPathNavigator to navigate across other hierarchies
    • Extend XmlReader/XmlWriter to produce/consume XML
    • Extend XslTransform to include custom behavior
extending xmldocument
Extending XmlDocument
  • XmlDocument is a concrete, non-sealed class
    • LoadXml() parses XML into Infoset form
    • Uses various CreateXXX() methods to create the Infoset tree
    • Some methods are marked virtual for easy overriding
    • Allow for creation of objects which can plug into Infoset tree
extending xmldocument1
Extending XmlDocument
  • Start with your basic Person
extending xmldocument2
Extending XmlDocument
  • We want to create Persons from XML
extending xmldocument3
Extending XmlDocument
  • Solution: Person IS-A XmlElement...
extending xmldocument4
Extending XmlDocument
  • ... and we use a custom XmlDocument to create them
extending xmldocument5
Extending XmlDocument
  • ... and now we can create Persons from XML
extending xmldocument6
Extending XmlDocument
  • So what?
    • We can already do XML-to-object-to-XML mappings with xsd.exe
    • Why bother overriding XmlDocument?
      • No schema definition required
      • Flexible creation: what if we want to change types based on the XML?
      • We want to work with both XML and type representations at once
      • Allows for objects to be extensible; unrecognized elements (age? SSN?) are still part of Person, just not captured strongly
    • Requires extending XmlElement (implementation inheritance)
extending xpathnavigator
Extending XPathNavigator
  • XPathNavigator: abstract class providing XPath cursor
    • Provides ability to execute XPath queries on hierarchical storage
    • XPathDocument provides XPathNavigator for XmlDocument objects
    • Extend XPathNavigator and override as necessary to provide customization
    • (See Aaron Skonnard’s “XML Files” article in MSDN for examples)
extending xpathnavigator1
Extending XPathNavigator
  • So what?
    • XPath provides powerful hierarchical query API
    • Extend that to other hierarchical storage systems for ease-of-use
    • Because XslTransform takes XPathNavigator as source argument, use custom XPathNavigator to do transforms on non-XML sources
extending xmlreader xmlwriter
Extending XmlReader/XmlWriter
  • XmlReader and XmlWriter
    • “Source” and “sink” for XML, respectively
    • Abstract base classes with numerous abstract methods
  • XmlTextReader and XmlTextWriter
    • Derivatives of XmlReader and XmlWriter, respectively
    • Specifically deal with producing/consuming XML from text streams
    • Useful as templates for creating customized reader/writer classes
extending xmlreader xmlwriter1
Extending XmlReader/XmlWriter
  • So what?
    • We have XmlTextReader/XmlTextWriter, what else do we need?
    • XML may come in forms other than plain text
      • encrypted
      • compressed
    • XML may come from other sources than files
      • Fixed-length flat files
      • CSV files
    • XML could be processed entirely in-proc: no storage whatsoever
extending xsltransform
Extending XslTransform
  • XslTransform does XSLT processing programmatically
    • Create an XslTransform object
    • Call Transform(), passing in source and output:
extending xsltransform1
Extending XslTransform
  • Two ways to add behavior to XSLT processing:
    • Add extensions as “script” within XSLT stylesheet itself
    • Add objects to XSLT arguments passed into Transform()
extending xsltransform2
Extending XslTransform
  • Add script extensions to stylesheet
extending xsltransform3
Extending XslTransform
  • Add objects to argument list to Transform()
    • First, create an “extension object”: methods will be called from XSLT
extending xsltransform4
Extending XslTransform
  • Add objects to argument list to Transform()
    • Add extension objects to XsltArgumentList and pass to Transform()
extending xsltransform5
Extending XslTransform
  • Add objects to argument list to Transform()
    • Use the called method in the stylesheet
extending xsltransform6
Extending XslTransform
  • So what?
    • XSLT has a lot of built-in behavior; what more do we need?
    • Extensions can provide additional “reach” to XSLT
      • database
      • network (FTP, HTTP, WebServices/SOAP, ...)
      • other web services
    • Extensions can also provide similar-yet-different behavior
      • Such as concat-with-whitespace, concat-without-whitespace, etc.
      • Best of both worlds: XSLT + the .NET FCL
  • Extending System.Xml is a lot easier than you might think
  • Look for ways to use this flexibility
    • Custom XmlDocument types to provide type-safety and Infoset APIs
    • Custom XPathNavigators to allow for easy access and XSLT transformation
    • Custom XML sources and sinks
    • Custom behavior in XSL transformations
  • Who is this guy?
    • Independent consultant
    • Author
      • C# in a Nutshell (O’Reilly, with Drayton, Albahari, 2001)
      • Server-Based Java Programming (Manning, 2000)
      • SSCLI Essentials (O’Reilly, with Stutz, Shilling, 2003)
      • Effective Enterprise Java (Addison-Wesley, 3Q 2003)
    • Papers at http://www.neward.net
    • Blog at http://blogs.tedneward.com