0

我实际上正在更新一个旧版应用程序VB 6.0,我需要将一个元素添加到声明为IXMLDOMElement. 我的 XML 对象的内容实际上如下;

<ChoiceLists>
 <Test Default="*">
  <Choice Value="1" Description="Hours"/>
  <Choice Value="2" Description="Days"/>
 </Test>
</ChoiceLists>

现在我有一个查询,它已经返回了一个 XML 格式的结果(作为字符串),如

<Test2 Default="*">
  <Choice Value="276" Description="#276"/>
  <Choice Value="177" Description="#177"/>
  <Choice Value="0000" Description="#0000"/>
  <Choice Value="176" Description="#176"/>
</Test2>

我需要将其集成到我的 XML 中,即在 root node<ChoiceLists>中。

谁能告诉我如何将此字符串添加到我的 XML 中?我一直在尝试IXMLDOMElement对象的不同功能,但徒劳无功。

谢谢

4

1 回答 1

2

您可以使用该IXMLDOMNode.appendChild()方法将一个元素(和子元素)添加到另一个元素。如果您有一个需要转换的原始字符串,您可以将其加载到新的DOMDocument使用中IXMLDOMDocument.loadXML()

Dim TargetDocument As IXMLDOMDocument
Dim TargetElement As IXMLDOMElement
Dim NewDocument As IXMLDOMDocument
Dim NewElement As IXMLDOMElement

'Load your target document here
Set TargetDocument = New DOMDocument
TargetDocument.Load "P:\iCatcher Console\XML\feedlist.xml"

'Get a reference to the element we want to append to (I'm assuming it's the document element)
Set TargetElement = TargetDocument.DocumentElement

'Create a new documents to parse the XML string
Set NewDocument = New DOMDocument
NewDocument.loadXML NewXMLString

'The root of this document will be the outer element in the string so get a reference to that
Set NewElement = NewDocument.DocumentElement

'Append the new element to the target's children
TargetElement.appendChild NewElement

生成的 XML 现在将类似于:

<ChoiceLists>
 <Test Default="*">
  <Choice Value="1" Description="Hours"/>
  <Choice Value="2" Description="Days"/>
 </Test>
 <Test2 Default="*">
  <Choice Value="276" Description="#276"/>
  <Choice Value="177" Description="#177"/>
  <Choice Value="0000" Description="#0000"/>
  <Choice Value="176" Description="#176"/>
 </Test2>
</ChoiceLists>
于 2014-09-16T12:25:22.017 回答