Microsoft KB Archive/317728

From BetaArchive Wiki

PSS ID Number: 317728

Article Last Modified on 2/9/2003



The information in this article applies to:

  • Microsoft .NET Framework Class Libraries
  • Microsoft Visual Basic .NET (2002)



This article was previously published under Q317728

For a Microsoft Visual C# .NET version of this article, see 317750.

IN THIS TASK

SUMMARY

This step-by-step article demonstrates how to use Microsoft XML Core Services 4.0 (MSXML) in Visual Basic .NET projects by using the COM interoperability features of .NET.

The Microsoft .NET Framework provides ways to interoperate with the existing COM components. Because MSXML 4.0 is COM-based, when you use MSXML with the .NET applications, MSXML runs as unmanaged code outside the common language runtime (CLR).

Applications that you develop solely with System.Xml provide numerous benefits, such as interoperability with ADO.NET components, scalability, and other enhancements that System.Xml and the .NET Framework offer. However, you may want to use MSXML 4.0 in .NET to do the following:

  • Port the existing code with minimal changes and use the current skillset.
  • Work with an existing code base that uses Simple API for XML (SAX) or msxsl:script elements (with older scripting languages) in Extensible Stylesheet Language Transformations (XSLT).


NOTE: System.Xml supports scripting with the CLR compliant languages.

  • Interoperate with MSXML 3.0 to use the older XSL stylesheet standard.
  • Overcome the XSLT performance issue with the System.Xml.XslTransform class.

Please refer to the following knowledge base article for more information about the XSLT performance issue:

317691 XSL Transformations may perform slower with System.Xml than MSXML 4.0


For more information about interoperability in the .NET Framework, refer to the links in the "References" section of this article.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:

  • Microsoft Windows XP, Microsoft Windows 2000, or Microsoft Windows NT 4.0 Service Pack 6a
  • Microsoft Data Access Components 2.6 (MDAC) or later
  • Microsoft Visual Studio .NET
  • MSXML 4.0

This article assumes that you are familiar with the following topics:

  • Visual Basic .NET syntax
  • XML and the related standards
  • Using MSXML 4.0

back to the top

Import MSXML 4.0 Type Library as an Assembly

You must convert COM type definitions in the type libraries into metadata of assemblies. .NET provides several ways to convert COM type definitions. For detailed information, refer to the topic, "Importing a Type Library as an Assembly," in the .NET Framework Developer's Guide.

Typically, you can generate this metadata when you use the Visual Studio .NET integrated development environment (IDE) or the Type Library Importer (Tlbimp.exe) command line tool to add a project reference to the native DLL. This article examines these methods with MSXML 4.0. You can use the other methods as provided in the documentation, also.

  • Using Visual Studio .NET


To add a reference to MSXML 4.0 in your project, follow these steps.

NOTE: This procedure automatically creates an assembly named "Interop.MSXML2.dll" in the bin directory of your application.

  • *# From the Project menu, click Add Reference.Click the COM tab.Click the Microsoft XML, 4.0 component, and then click Select. The Selected Components pane displays the MSXML 4.0 entry.Click OK.
    1. Click the COM tab.Click the Microsoft XML, 4.0 component, and then click Select. The Selected Components pane displays the MSXML 4.0 entry.Click OK.
    2. Click the Microsoft XML, 4.0 component, and then click Select. The Selected Components pane displays the MSXML 4.0 entry.Click OK.
    3. Click OK.
  • Using the Type Library Importer

    Use the following command to create an assembly from MSXML 4.0 native DLL. To run this assembly, point the Path environment variable to the installation location of Tlbimp.exe or explicitly specify the full path to Tlbimp.exe. The output assembly name can be any valid name; this example uses the name, "Msxml4net.dll" (this name is used as the namespace name, also).

    c:\>tlbimp "c:\winnt\system32\msxml4.dll" /out:"c:\winnt\system32\msxml4net.dll"

    NOTE: The following warning, or any similar warning that you may receive during the conversion, is related to SAX. You must use unsafe code to handle the method specified in the warning.

    TlbImp warning: At least one of the arguments for 'InterfaceName.MethodName' can not be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate.

    After you create the assembly, you can use the COM types in this new assembly in managed clients, just as you use any other managed type. You can obtain and release a reference of these COM types in a similar manner to that of managed types, and the runtime handles the reference count.

    You can use a type library importer to marshal MSXML objects such as DOM documents between .NET processes, in which case you must use the same assembly.

back to the top

Steps to Create a Sample Application

The following code example demonstrates how you can intertwine MSXML 4.0 in .NET. This code example performs the following tasks:

  1. Use MSXML 4.0 to transform the XML.Load the results into an XmlDocument object.Run an XPath query with MSXML 4.0 to collect data.Add the collected data to a new XmDocument instance.
  2. Load the results into an XmlDocument object.Run an XPath query with MSXML 4.0 to collect data.Add the collected data to a new XmDocument instance.
  3. Run an XPath query with MSXML 4.0 to collect data.Add the collected data to a new XmDocument instance.
  4. Add the collected data to a new XmDocument instance.

One caveat is that the System.Xml objects and the MSXML COM objects are not interchangeable; for example, casting an IXMLDOMNode instance to a System.Xml.XmlNode instance does not work.

  1. Use Notepad or a similar text editor to save the following data as a file named "Q317728.xml":

    <?xml version='1.0' encoding='ISO-8859-1'?>
    <Collection>
       <Book>
          <Title>Principle of Relativity</Title>
          <Author>Albert Einstein</Author>
          <Genre>Physics</Genre>
       </Book>
       <Book>
          <Title>Cosmos</Title>
          <Author>Carl Sagan</Author>
          <Genre>Cosmology</Genre>
       </Book>
    </Collection>
  2. Save the following data as a file named "Q317728.xsl". This XSL style sheet returns a copy of the same XML data:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="/ | @* | node()">
       <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
       </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>
  3. Create a new Visual Basic .NET Console Application project. Set a reference to MSXML 4.0, as explained in the section, "Using Visual Studio .NET," so that the code uses the MSXML2 namespace. If you want to use the Msxml4net.dll assembly, set a reference to the Msxml4net.dll assembly and then use the MSXML4NET namespace.
  4. Replace the code in Module1.vb with the following sample code.

    NOTE: With Interop, COM classes are exposed as both interfaces and classes. Classes have the "Class" extension appended to the interface name. One difference is that the interfaces cannot act as base classes. This code sample uses class syntax, for example DOMDocument40Class, whereas you can use the interface syntax DOMDocument40, also.

    Imports MSXML2
    Imports System.Xml
    Imports System.IO
    
    Module Module1
    
       Sub Main()
    
          Try
             ' 1. Working with MSXML 4.0 DOM Documents.
             Dim msXmlDoc As New DOMDocument40Class()
             Dim msXslDoc As New DOMDocument40Class()
    
             ' Load the XML and XSL data.
             Dim isSuccess As Boolean = False
             isSuccess = msXmlDoc.load("Q317728.xml")
             If isSuccess = False Then
                Throw New Exception(msXmlDoc.parseError.errorCode & _
                                      " " & msXmlDoc.parseError.reason)
             End If
    
             isSuccess = msXslDoc.load("Q317728.xsl")
             If isSuccess = False Then
                Throw New Exception(msXslDoc.parseError.errorCode & _
                                      " " & msXslDoc.parseError.reason)
             End If
    
             ' 2. Transform XML by using MSXML 4.0, and then load the results into XmlDocument.
             Dim netXmlDoc As New System.Xml.XmlDocument()
             netXmlDoc.LoadXml(msXmlDoc.transformNode(msXslDoc))
    
             ' Display the content of the DOM document.
             Console.Write("{0}", vbNewLine & netXmlDoc.OuterXml & vbNewLine)
    
             ' 3. Run an XPath query with MSXML 4.0, and then process the results.
             Dim msXmlNodeList As IXMLDOMNodeList
             msXmlNodeList = msXmlDoc.selectNodes("//Book/Title")
    
             ' Display the results.
             Dim i As Integer
             For i = 0 To msXmlNodeList.length - 1
                Console.Write("{0}", vbNewLine & msXmlNodeList.item(i).xml)
             Next
    
             ' 4. Create a new System.Xml.XmlDocument from the above MSXML node list.
             netXmlDoc.LoadXml("<BookTitles></BookTitles>")
             Dim msXmlNode As IXMLDOMNode
             Dim newElem As XmlNode
             For i = 0 To msXmlNodeList.length - 1
                msXmlNode = msXmlNodeList.item(i)
                newElem = netXmlDoc.CreateNode(XmlNodeType.Element, msXmlNode.nodeName, Nothing)
                newElem.InnerText = msXmlNode.text
                netXmlDoc.DocumentElement.AppendChild(newElem)
             Next
    
             ' Display the content of the new XmlDocument.
             Console.Write("{0}", vbNewLine & vbNewLine & netXmlDoc.OuterXml)
             Console.Write("{0}", vbNewLine & vbNewLine)
    
          Catch xmlex As XmlException                  ' Handle the Xml Exceptions here.
             Console.WriteLine("{0}", xmlex.Message)
          Catch ex As Exception                        ' Handle the generic Exceptions here.
             Console.WriteLine("{0}", ex.Message)
          Finally
             Console.ReadLine()
          End Try
    
       End Sub
    
    End Module
  5. Read the inline comments to understand the functionality of the code.
  6. Compile and run the application.

    NOTE: Either the XML and XSL files must be in the same directory as the executable or you must specify the path, such as "C:\ConsoleApps\XmlInterop\Q317728.xml".

    The output should look similar to the following.
<?xml version="1.0"?>
<Collection>
<Book><Title>Principle of Relativity</Title><Author>Albert Einstein</Author><Genre>Physics</Genre></Book>
<Book><Title>Cosmos</Title><Author>Carl Sagan</Author><Genre>Cosmology</Genre></Book>
</Collection>

<Title>Principle of Relativity</Title>
<Title>Cosmos</Title>

<BookTitles><Title>Principle of Relativity</Title><Title>Cosmos</Title></BookTitles>



back to the top

REFERENCES

For additional information, see the following Microsoft Knowledge Base article and Microsoft .NET Framework Software Development Kit (SDK) documentation:

313651 INFO: Roadmap for XML in the .NET Framework


back to the top

Keywords: kbBCL kbCOMInterop kbhowto kbHOWTOmaster KB317728
Technology: kbAudDeveloper kbNFClassLibSearch kbVBNET kbVBNETSearch kbVBSearch