VBA add child and 2 attributes
-
This is a snippet of an xml file I need to add a child to:
403131A1-95D CA Lily lily@13.com
I need to add "CEmail2", CEmail3" etc, etc to this group. Using a xml file viewer I manually did this by doing 3 steps: Add child to named "prop" Add attribute to prop Name="Name",Value="CEmail2" add attribute to prop Name="type', Value="0" Currently, I am writing to existing nodes using the following:
Dim DOM As MSXML2.DOMDocument
Dim Node As MSXML2.IXMLDOMNode
Set DOM = New MSXML2.DOMDocumentWith DOM
.async = False
.preserveWhiteSpace = True
If .Load("C:\HouseMaster\CloneRPT.hr4") Then
.setProperty "SelectionLanguage", "XPath"
Set Node = .SelectSingleNode("//ClientData/prop[@name='CEmail']")
Node.Text = "Someone@aol.com"End If End With
I believe that I neded to use CreateChild() but how do I add the attributes as I did manually in VBA?
Thanks for your help -
This is a snippet of an xml file I need to add a child to:
403131A1-95D CA Lily lily@13.com
I need to add "CEmail2", CEmail3" etc, etc to this group. Using a xml file viewer I manually did this by doing 3 steps: Add child to named "prop" Add attribute to prop Name="Name",Value="CEmail2" add attribute to prop Name="type', Value="0" Currently, I am writing to existing nodes using the following:
Dim DOM As MSXML2.DOMDocument
Dim Node As MSXML2.IXMLDOMNode
Set DOM = New MSXML2.DOMDocumentWith DOM
.async = False
.preserveWhiteSpace = True
If .Load("C:\HouseMaster\CloneRPT.hr4") Then
.setProperty "SelectionLanguage", "XPath"
Set Node = .SelectSingleNode("//ClientData/prop[@name='CEmail']")
Node.Text = "Someone@aol.com"End If End With
I believe that I neded to use CreateChild() but how do I add the attributes as I did manually in VBA?
Thanks for your helpIt's been a while, but try something like this:
Dim ClientData As MSXML2.IXMLDOMNode
Set ClientData = .SelectSingleNode("//ClientData")Set Node = .CreateElement("prop")
Node.Text = "chunkylover53@aol.com"
Node.SetAttribute "name", "CEmail2"
Node.SetAttribute "type", "0"
ClientData.AppendChild NodeThe API reference is archived, but still available: DOM Reference | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
It's been a while, but try something like this:
Dim ClientData As MSXML2.IXMLDOMNode
Set ClientData = .SelectSingleNode("//ClientData")Set Node = .CreateElement("prop")
Node.Text = "chunkylover53@aol.com"
Node.SetAttribute "name", "CEmail2"
Node.SetAttribute "type", "0"
ClientData.AppendChild NodeThe API reference is archived, but still available: DOM Reference | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer