Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VBA add child and 2 attributes

VBA add child and 2 attributes

Scheduled Pinned Locked Moved Visual Basic
xmlquestionhtmlcom
3 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mhnCPF
    wrote on last edited by
    #1

    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.DOMDocument

    With 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

    Richard DeemingR 1 Reply Last reply
    0
    • M mhnCPF

      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.DOMDocument

      With 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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 Node

      The 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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 Node

        The 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

        M Offline
        M Offline
        mhnCPF
        wrote on last edited by
        #3

        Richard, thanks for the quick reply. Michael :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups