Link to home
Start Free TrialLog in
Avatar of ceMo89
ceMo89

asked on

VB.NET XML-Document

Hey guy's,

I've a question about VB.NET and XML.
I'm be able to create a new XML-Document.
My question: How can I add some nodes to the already existing XML-Document???

Thanks.
'Sub to create the XML-Document
Private Sub writeXML()
 
        Dim writer As XmlWriter
        writer = XmlWriter.Create("properties.xml")
        writer.WriteStartDocument()
        writer.WriteStartElement("Drinks")
 
    
        writer.WriteStartElement("Drink")
        writer.WriteAttributeString("ID","1")
        writer.WriteElementString("Name", "Blubb")
        writer.WriteElementString("Prize", "10")
        writer.WriteElementString("Amount", "4")
        writer.WriteElementString("Path", "Logo.gif")
        writer.WriteEndElement()
 
        writer.WriteEndDocument()
        writer.Close()
 
        Return Nothing
End Sub
---------------------------------------------------------------------------
'Result looks like this:
  <?xml version="1.0" encoding="utf-8" ?> 
- <Drinks>
   - <Drink ID="1">
        <Name>Blubb</Name> 
        <Prize>10</Prize> 
        <Amount>4</Amount> 
        <Path>Logo.gif</Path> 
     </Drink>
  </Drinks>
----------------------------------------------------------------------------
'I want to add a new node...so the result should looks like this:
  <?xml version="1.0" encoding="utf-8" ?> 
- <Drinks>
   - <Drink ID="1">
        <Name>Blubb</Name> 
        <Prize>10</Prize> 
        <Amount>4</Amount> 
        <Path>Logo.gif</Path> 
     </Drink>
   - <Drink ID="2">
        <Name>Bla</Name> 
        <Prize>5</Prize> 
        <Amount>1</Amount> 
        <Path>Logo2.gif</Path> 
     </Drink>
  </Drinks>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of cauos
cauos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of barrislb
barrislb

Hi ceMo89,

I believe this code may be some help to you.  
Private Sub InsertNewNode()
        '-- Declare local variable and load xml file
        Dim doc As New XmlDocument()
        doc.Load("C:\test.xml")
 
        '-- Create a new node and add it to the document.
        Dim Drink As XmlElement = doc.CreateElement("Drink")  
        Drink.SetAttribute("ID", "IdNum")        
       
        '-- Name node
        Dim Name As XmlElement = doc.CreateElement("Name")
        Name.InnerText = "Enter a Name"
        '-- Prize node
        Dim Prize As XmlElement = doc.CreateElement("Prize")
        Prize.InnerText = "Enter a Prize"
        '-- Amount node
        Dim Amount As XmlElement = doc.CreateElement("Amount")
        Amount.InnerText = "Enter the Amount"
        '-- Path node
        Dim Path As XmlElement = doc.CreateElement("Path")
        Path.InnerText = "Enter a Path"
 
        '-- Append the new node to the end of the file.
        doc.DocumentElement.AppendChild(Drink)
        doc.DocumentElement.LastChild.AppendChild(Name)
        doc.DocumentElement.LastChild.AppendChild(Prize)
        doc.DocumentElement.LastChild.AppendChild(Amount)
        doc.DocumentElement.LastChild.AppendChild(Path)
 
        '-- Save the xml file with the new changes.
        doc.Save("C:\test.xml")
        MessageBox.Show("New xml node has been added.", "XML Node Add...", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

Open in new window