I am trying to convert this function that was created years ago in VB6 to C# but I am not sure how to go about it. In this VB6 code, I am confused on how to use MSXML2.DOMDocument40
as a parameter in C#. I'm sure it's going to require the use of XmlDocument
instead.
Private Function m_LoadXML(xmlDoc As MSXML2.DOMDocument40, xmlRoot As MSXML2.IXMLDOMElement, strXMLScript As String) As Boolean
On Error GoTo Proc_Error
Dim blnResult As Boolean
Dim strMsg As String
blnResult = False
Set xmlDoc = New MSXML2.DOMDocument40
If xmlDoc.Load(strXMLScript) Then
Set xmlRoot = SafeElementNode(xmlDoc, mcstrXmlNodeRoot)
If Not xmlRoot Is Nothing Then
blnResult = True
Else
strMsg = "Invalid XML database definition file " & strXMLScript
RaiseEvent ErrorMessage(strMsg)
End If
Else
strMsg = "Can not load XML database definition file " & strXMLScript
RaiseEvent ErrorMessage(strMsg)
End If
Proc_Exit:
On Error Resume Next
m_LoadXML = blnResult
Exit Function
Proc_Error:
RaiseEvent ErrorMessage(Error)
Resume Proc_Exit
End Function
Public Function SafeElementNode( _
vxmlDocumentOrElement As MSXML2.IXMLDOMNode, _
vstrQueryString As String _
) As MSXML2.IXMLDOMNode
On Error Resume Next
Set SafeElementNode =
vxmlDocumentOrElement.selectSingleNode(vstrQueryString)
End Function
The following is what I have come up with so far:
private bool m_LoadXML(XmlElement xmlRoot, string strXMLScript)
{
bool blnResult = false;
string strMsg;
var xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(strXMLScript);
xmlRoot = xmlDoc.DocumentElement;
if (xmlRoot != null) blnResult = true;
else
{
strMsg = "Invalid XML database definition file " + strXMLScript;
MessageBox.Show(strMsg);
}
}
catch (Exception)
{
strMsg = "Can not load XML database definition file " + strXMLScript;
MessageBox.Show(strMsg);
throw;
}
return blnResult;
}