【问题标题】:Converting XML manipulation code from VB6 into C#将 XML 操作代码从 VB6 转换为 C#
【发布时间】:2018-04-10 16:38:06
【问题描述】:

我正在尝试将多年前在 VB6 中创建的这个函数转换为 C#,但我不知道如何去做。在这个 VB6 代码中,我对如何在 C# 中使用 MSXML2.DOMDocument40 作为参数感到困惑。我确定它需要使用XmlDocument

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

以下是我目前想出的:

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;
}

【问题讨论】:

  • 你应该使用XDocument
  • 我实际上已经尝试过那个转换器了。
  • C# 具有专用的 XML 处理功能,这些功能在 VB6 时代不可用。您将从重写中受益匪浅,不仅是这个函数,整个类也是如此,以便利用这一点,这样您就永远不会有任何与MSXML2 相关的对象需要处理首先是。尝试将遗留 XML 代码转换为 .Net 将是……痛苦……以至于重写也可能花费更少的时间。
  • 也就是说……我们至少需要知道什么不起作用,您到目前为止已经尝试过什么。我在您的尝试中注意到了一件事:VB 代码似乎使用 xmlDocxmlRoot 参数作为 output 参数(我不确定这是如何工作的,因为 VB 默认为 @ 987654330@,你需要ByRef 才能工作)。在 C# 中,您必须使用参数声明显式声明 refout 才能获得该行为。

标签: c# xml code-conversion


【解决方案1】:

我认为这应该可行。您拥有的现有代码没有将 xmlRoot 变量声明为 out 参数,因此您最终会得到一个 null 根元素。

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);
}

正如其他评论员所建议的那样,XDocument 非常好用,值得一玩。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多