Solved

VB.NET XML-Document

Posted on 2009-05-04
3
718 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:ceMo89
3 Comments
 
LVL 4

Accepted Solution

by:
cauos earned 500 total points
ID: 24294166
you can load the XML file to XmlDocument  and append new XML node to the end of the file and then save this file again.
public sub AddNewNode()
 

        dim doc as New XmlDocument ()

        doc.Load("properties.xml")

 

        dim root as XmlNode = doc.DocumentElement

 

        //Create a new node.

        dim elem as XmlElement = doc.CreateElement("Drink")

        elem.InnerXml = "<yourNodes>mynode</yourNodes>"

 

        //Add the node to the document.

        root.AppendChild(elem)       

        doc.Save("properties.xml")

end sub

Open in new window

0
 
LVL 4

Expert Comment

by:barrislb
ID: 24303046
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Setting runtime form location 4 31
vb.net checkbox 7 41
Want to create a userform that looks like the image provided 10 46
Help with LINQ and XML 10 27
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now