Avatar of arthurh88
arthurh88
 asked on

Code to insert an XML line VB.NET 2005

Here is my sample XML file
<doc>
  <total_loops value="0" />
  <total_cmds value="0" />
  <total_macros value="5" />
  <loop1 name="MyLoop1" type="Min" length="20" data="Sample XML File" />
  <loop2 name="MyLoop2" type="Sec" length="50" data="some more data" />
  <macro1 name ="My Macro" Value ="100-245,C,W1,200-340,DC" />
</doc>

Im trying to get this function to work in order to insert a new nodelist after any particular nodename

Public Function InsertNode(ByVal FilePath As String, ByVal NodeXML As String, byVal AfterThisNode as string) As Boolean
        Dim doc As New System.Xml.XmlDocument
        doc.Load(FilePath)
        Dim nd As System.Xml.XmlNode
        nd = doc.DocumentElement.GetElementsByTagName(AfterThisNode).Item(0)
        If nd Is Nothing Then 'could not find the node to insert after, then just add to the bottom
        'TO DO:  need code to add the line of XML to the end of the document
        ELSE 'found the node, now insert new line after
        TO DO:  need code to insert XML line after found node
        End If
        doc.Save(FilePath)
        Return True
    End Function
.NET ProgrammingVisual Basic.NETASP.NET

Avatar of undefined
Last Comment
arthurh88

8/22/2022 - Mon
TheNige

I believe that you could just use this:

doc.InsertAfter(myNewNode, nd)



arthurh88

ASKER
im trying to insert a node list.  
for example,  <loop1a name="MyLoop1a" type="Sec" length="34" data="data" /> to be inserted after loop1

doc.InsertAfter(myNewNode, nd) returns an error.  How do I set myNewNode to equal  "<loop1a name="MyLoop1a" type="Sec" length="34" data="data" />"
ASKER CERTIFIED SOLUTION
TheNige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
arthurh88

ASKER
yes that worked.  had to use
 doc.DocumentElement.InsertAfter(myNewNode, nd)  instead of
doc.InsertAfter(myNewNode, nd)

dont really understand why, but it works nontheless.  thanks
Your help has saved me hundreds of hours of internet surfing.
fblack61