无法将类型为“System .__ ComObject”的COM对象强制转换为类类型“System.Xml.XmlDocument”
我有一个com组件,我已在我的vb.net项目中注册并添加为参考。在尝试访问其方法时,我收到上述错误。需要你的帮助。
以下是代码片段
Dim testResult As Object = Nothing
Dim oxmldom As Object
Dim sXML As String
sXML =
testResult = CreateObject(ComReferenceDLL.iInterface1 )
oxmldom = CreateObject(MSXML2.DOMDocument)
oxmldom = testResult.SampleMethod(sXML)
这里ComReferenceDLL是COM组件。
I have a com component, which i have registered and added as reference in my vb.net project. While trying to access its method I am getting the above error. Need your help.
Below is the code snippet
Dim testResult As Object = Nothing
Dim oxmldom As Object
Dim sXML As String
sXML = ""
testResult = CreateObject("ComReferenceDLL.iInterface1")
oxmldom = CreateObject("MSXML2.DOMDocument")
oxmldom = testResult.SampleMethod(sXML)
Here ComReferenceDLL is the COM component.
当然你不能。谁告诉你这些类型是以某种方式相关的,忘了它的赋值兼容性。
你不应该将旧的不太好的COM与.NET FCL类混合使用,已经为您提供阅读,解析和生成XML所需的一切。这是我对它们的简短概述:
Of course you cannot. Who told you these type are related somehow, forget about its assignment compatibility.
You should not mix old-not-so-good COM with .NET FCL classes, which already provide you will all you need to reading, parsing and generating XML. This is my short overview of them:
- 使用
System.Xml.XmlDocument
类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。 - 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。 - 使用类
System.Xml.Linq.XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]. - Use the class
System.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^]. - Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].