|
Determine Performance Requirements (Continued)
EnumerateWin32Threads does more than execute queries; it also processes each Win32_Thread instance by appending its XML into an XmlDocument container. Typically, you cannot append XML fragments to the root of the XmlDocument directly because doing so more than once violates a document’s requirement of having only one root element. Creating a single document element node in the XmlDocument container now allows adding multiple fragments as child nodes, where each fragment represents a thread instance and is well-balanced.
The XML is retrieved as a string using ManagementObject’s GetText method using a TextFormat of CimDtd20. CimDtd20 and WmiDtd20 are each minor schema variations on the other, and it’s a moot choice for WmiGridView. The XmlReader converts an XML string to an XmlDocument. Before you can append the new node tree to the primary document, you must call ImportNode to alter its owner document.
Note that the sample creates the XmlReader with .NET 2.0’s new creational pattern. In this case, you prepare the XmlReaderSettings as a kind of profile for the reader implementation. You take this profile and pass it to the Create factory method, which is supposed to hand back an XmlReader implementation optimized for this profile.
Examining the natural XML representation of the thread instances can help you understand the mapping wmi2ds.xslt will do (see Listing 1). Copy EnumerateWin32Threads into default.aspx.cs, and call it from the Page_Load event handler (if you have no Page_Load event handler, you can switch to design view and double-click anywhere on the page to add one). Before calling EnumerateWin32Threads, remember to initialize the XmlDocument and add a single document element to it. Next, drag an ASP.NET Label onto the design view and then add this line after the call to EnumerateWin32Threads:
Label1.Text = doc.OuterXml.Replace(
"<","<").Replace(">","><br>");
This code displays the original XML representation in your browser when you run it with a quick and dirty XML viewer that works by escaping the < character. While well structured, this approach isn’t quite suitable for displaying tabular data in a WmiGridView at this point in the sample. WmiGridView needs more information; the missing data includes both simple element content and attribute values. You could use the ManagementObjects enumerated here to poke the needed information into DataTables, but XSLT can eliminate this manual drudgery.
Transform the DataSet
XSLT is a standard XML-related technology especially effective for mapping one XML source document structure to another XML result document structure. For example, examine the wmi2ds.xslt stylesheet (see Listing 2). Most stylesheets consist of two types of XML elements. Elements controlling how the transformation is performed reside in the “http://www.w3.org/1999/XSL/Transform” namespace URI, and conventionally have the prefix “xsl.” All other elements are literal XML that’s transferred verbatim into the resulting XML document. As in procedure-oriented programs, the stylesheet is divided into simple templates that get applied successively, starting with the root template—which matches the root (/) of the source XML document coming from WMI. Each template emits XML into the resulting XML document and applies further templates until the transformation is complete. Note that the expression in the select attribute matches the expressions in match attributes of other templates.
This stylesheet has a few more characteristics worth taking a closer look at. First, note how it uses the xsl:element tag. Each DataRow in the resulting document must have the same name as its DataTable. For WMI instances, this name comes from the CLASSNAME attribute of the source document. You must use xsl:element to specify the tag name before you can use the CLASSNAME attribute value as the name of an XML element in the resulting document. Using the name attribute on xsl:element allows you to specify an interpolative XPath expression (because of the curly braces in the name attribute value) that looks up the corresponding CLASSNAME attribute from the source document and uses its value.
Back to top
|