-1

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;
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • maybe this would be helpful https://visualstudiomagazine.com/Blogs/Tool-Tracker/2014/04/Convert-C-sharp-to-vb-or-vb-to-C-sharp.aspx – Anirudha Gupta Apr 10 '18 at 16:38
  • 2
    You should use `XDocument`. – SLaks Apr 10 '18 at 16:40
  • I actually tried that converter already. – Joseph Thompson Apr 10 '18 at 16:45
  • 2
    C# has dedicated XML handling features that were not available in the VB6-era. You would benefit significantly from **re-writing** not just this function, the entire class as well, in order to take advantage of this, such that you'll never have any `MSXML2`-related objects to deal with in the first place. Attempting to convert the legacy XML code to .Net will be... painful... to the point that the re-write is also likely to take much less time. – Joel Coehoorn Apr 10 '18 at 18:15
  • 1
    That said... we need to at least know **what isn't working** with what you've tried so far. One thing I did notice in your attempt: the VB-code seems to use `xmlDoc` and `xmlRoot` parameters as **output** parameters (I'm not sure how this ever worked, as VB defaults to `ByVal`, and you'd need `ByRef` there for it to work). In C#, you have to explicitly declare `ref` or `out` with the parameter declaration to get that behavior. – Joel Coehoorn Apr 10 '18 at 18:19
  • @JoelCoehoorn Unlike VB.NET, VB6 defaults to `ByRef`. – GSerg Apr 10 '18 at 18:33
  • I saw this line in the VB6 code: `Set xmlDoc = New MSXML2.DOMDocument40` so I removed one of the parameters since it seemed like it wasn't being used and set as a new xml doc anyways. Is that correct? – Joseph Thompson Apr 10 '18 at 18:41
  • @JosephThompson Like Joel Coehoorn said, that is an output parameter. You decide whether you need the function to return that. Probably not, because you are returning `xmlRoot` and it already links to the document. – GSerg Apr 10 '18 at 18:43
  • Downvoted; does not ask a question – Tom W Apr 13 '18 at 10:26

1 Answers1

0

I think something this should work. The existing code you have doesn't declare the xmlRoot variable as an out parameter, so you would end up with a null root element.

private bool m_LoadXML(out 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;
}

private void TestLoadXml(string xmlFile)
{
    XmlElement elem;
    var result = m_LoadXML(out elem, xmlFile);
}

Like the other commentators have suggested, XDocument is very good to work with, it's worth playing around with.

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40