1

我正在尝试了解 MSXML 及其用途。我有一个项目,我需要从 xml 文件中读取一个节点及其所有内容,然后将该节点复制到另一个 xml 文件。我被困在附加子节点上。我认为我做事正确,但我不断收到错误“对象不支持此属性或方法”。

它挂在“xNode2.appendChild(xElement)”上

我附上了我正在处理的程序。

如果有人能给我一些指导,将不胜感激。

Dim xDoc As DOMDocument60, xDoc2 As DOMDocument60, xNode As IXMLDOMElement, _
    xmlStr As String, xSub As IXMLDOMNode, xNode2 As IXMLDOMElement

Set xDoc = New DOMDocument60

'Attempt to load the backup file for the selected printer.
If xDoc.Load("C:\Program Files\ID Technology\CiControl\Backup Files\" & _
              main.printerView.SelectedItem.Key & "\" & _
              Format(Date, "mm.dd.yy") & ".xml") Then

    'Find the message xml in the backup file.
    Set xNode = xDoc.selectSingleNode("//Messages/Message[Name='" & msgView.SelectedItem & "']")

    'Begin extracting all of the message xml.
    For Each xSub In xNode.childNodes            
        Debug.Print xSub.xml
        'Build the xml string.
        xmlStr = xmlStr & xSub.xml
    Next

    Set xDoc2 = New DOMDocument60

    xDoc2.Load ("C:\Program Files\ID Technology\CiControl\Backup Files\127.0.0.1\" _
                & Format(Date, "mm.dd.yy") & ".xml")

    'Set xNode2 = xDoc2.createElement("Message")
    Set xNode2 = xDoc2.selectSingleNode("//Messages")

    Dim xElement As IXMLDOMElement

    Set xElement = xDoc2.createElement("Message")

    xElement.Text = xmlStr

    xNode2.appendChild (xElement)
    'xDoc2.documentElement.appendChild (xNode2)

    xDoc2.save ("C:\Program Files\ID Technology\CiControl\Backup Files\127.0.0.1\" & _
                Format(Date, "mm.dd.yy") & ".xml")

End If

Set xDoc = Nothing
Set xNode = Nothing
Set xSub = Nothing
Set xDoc2 = Nothing
Set xNode2 = Nothing
4

1 回答 1

0

您需要先将节点从原始 DOM导入新 DOM,然后才能将节点添加到新 DOM。

Dim CloneNode As IXMLDOMNode
Set CloneNode = xDoc2.ImportNode(xElement)
xNode2.appendChild (CloneNode)
于 2013-12-03T16:48:40.390 回答