web analytics

The type or namespace name 'MSXML2' could not be found

Options

codeling 1602 - 6666
@2016-11-28 13:53:14

Compiling a C# program and get the following compiler error:

Error 173 The type or namespace name 'MSXML2' could not be found (are you missing a using directive or an assembly reference?) 

 

 

@2016-11-28 13:57:32

To create interop assembly MSXML2.DLL, use tool tlbimp.exe:

tlbimp c:\windows\system32\msxml6.dll

It will create MSXML2.dll in the current directory. Why MSXML2 was created out of MSXML6? The reason is taht msxml6.dll contains a type library named MSXML2. So tlbimp created a managed assembly MSXML2, which become also the DLL name.

The Type Library Importer converts the type definitions found within a COM type library into equivalent definitions in a common language runtime assembly. The output of Tlbimp.exe is a binary file (an assembly) that contains runtime metadata for the types defined within the original type library. You can examine this file with tools such as Ildasm.exe.

This tool is automatically installed with Visual Studio. To run the tool, use the Developer Command Prompt.

@2016-11-28 14:06:36

The PIA(Primay Interop Assembly) contains the official description of the types as defined by the COM publisher of those types. The PIA may contain certain customizations that make the types easier to use from managed code. The PIA is always signed by the publisher of the original COM type. If you publish a COM component, it is a good idea to provide a primary interop assembly, unless you don't want your component to be used from managed code.

Any interop assembly that is not provided by the publisher of the COM types is considered unofficial and should be avoided. Because the types defined in such an assembly are not to be signed by the publisher of the PIA, they are incompatible with the definitions provided in the PIA.

How do I make a PIA for my COM type library?

In most cases, creating a PIA is easy. The Tlbimp (Type Library Importer) tool provided in the Microsoft® .NET Framework SDK is capable of creating an interop assembly from an existing type library. The /primary switch on Tlbimp is used to create a PIA. All PIAs must be signed by their publisher so the publisher key must also be supplied with the /keyfile switch. For example:

TlbImp Widget.tlb /primary /keyfile:AcmeKey.snk /out:WidgetLib.dll

In the example above, Widget.tlb is the type library being imported, AcmeKey.snk is the keyfile containing the public key for the Acme Company, and WidgetLib.dll is the PIA that will be generated. A test keyfile can be generated with SN utility (SN.EXE —k AcmeKey.snk). Before the assembly is actually deployed, it should be signed with the company's real key. The same key should be used to sign all assemblies from a single company.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com