[英]how to add a child node to an xml in excel vba
我有這樣的xml模板
<?xml version="1.0" encoding="UTF-8"?>
<manageServiceProfileRequest xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<subscriptionsList>
<**subscription**>
<scode>##SCODE##</scode>
<sname>##SNAME##</sname>
<action>add</action>
</subscription>
</subscriptionsList>
</manageServiceProfileRequest>
我的要求是根據輸入作為輸入的訂閱數量添加任意數量的訂閱節點。 說如果訂閱數是5,那么我應該有5個,那么我的xml應該在subscriptionsList標記內有5個訂閱節點。 我編寫了代碼,在其中我手動創建了一個訂閱節點,並將scode,sname和action添加為子節點,然后追加到父節點。 我需要使用示例模板中的現有scubscription節點並將其添加任意次。
不確定我是否完全理解這個問題,但是如果您的文檔從一個subscription
節點開始,並且您需要添加額外的subscription
節點,則可以重復以下操作:深度克隆現有subscription
節點,並使新節點成為subscriptionsList
節點的子節點:
' Note the True parameter to indicate we want to do a deep clone
Set newNode = existingSubscriptionNode.cloneNode(True)
' change the values of scode, sname and action here if needed
existingSubscriptionNode.parentNode.appendChild newNode
在MSDN參考中有關cloneNode和appendChild的更多詳細信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.