Sunday, 20 July 2014

Need to add a node to an XML file in VBA without overwriting older node



I have a XML that I'm importing a set of notes and associating it to the current date and time node (Date NoteD) for example:



<Serial SerialN="3s">
<Model>3m</Model>
<Assigned>3a</Assigned>
<DateA>7/3/2014</DateA>
<DateR>N/A</DateR>
<Notes>
<Date NoteD="7/19/2014 07:58:42 PM">
<Entry>This is a test</Entry>
</Date>
</Notes>
</Serial>


I want to add another entry with a new current date and time so it appears like this:



<Notes>
<Date NoteD="7/19/2014 07:58:42 PM">
<Entry>This is a test</Entry>
</Date>
<Date NoteD="7/19/2014 09:45:00 PM">
<Entry>This is another test</Entry>
</Date>
</Notes>


Problem is that it keeps overwriting the date but adding another entry like this:



<Notes>
<Date NoteD="7/19/2014 09:45:00 PM">
<Entry>This is a test</Entry>
<Entry>This is another test</Entry>
</Date>
</Notes>


Here is my code:



Set x = objDom.SelectSingleNode("//Serial[@SerialN='" & TextBox1.Text & "']/Notes")
Set Datee = objDom.createElement("Date")
x.appendChild Datee


Set n = objDom.SelectSingleNode("//Serial[@SerialN='" & TextBox1.Text & "']/Notes/Date")
Set Notes = objDom.createAttribute("NoteD")
Notes.NodeValue = Now
n.setAttributeNode Notes


Set t = objDom.SelectSingleNode("//Serial[@SerialN='" & TextBox1.Text & "']/Notes/Date[@NoteD='" & Notes.NodeValue & "']")
Set Note = objDom.createElement("Entry")
t.appendChild Note
Note.Text = TextBox5.Text


Any help would be greatly appreciated!


No comments:

Post a Comment