250 likes | 438 Views
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.
E N D
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 • 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.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 • 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 XmlDocument • Start with your basic Person
Extending XmlDocument • We want to create Persons from XML
Extending XmlDocument • Solution: Person IS-A XmlElement...
Extending XmlDocument • ... and we use a custom XmlDocument to create them
Extending XmlDocument • ... and now we can create Persons from XML
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 • 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 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 • 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/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 • XslTransform does XSLT processing programmatically • Create an XslTransform object • Call Transform(), passing in source and output:
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 XslTransform • Add script extensions to stylesheet
Extending XslTransform • Add objects to argument list to Transform() • First, create an “extension object”: methods will be called from XSLT
Extending XslTransform • Add objects to argument list to Transform() • Add extension objects to XsltArgumentList and pass to Transform()
Extending XslTransform • Add objects to argument list to Transform() • Use the called method in the stylesheet
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
Summary • 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
Credentials • 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