Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i want to add new element for existing xml file programmatically..
i tried this code, which it is work but it doesn't do any change in my xml file.

hope you help me
thanks in advance

What I have tried:

VB
Private Sub newnode()
    Dim file As String = Server.MapPath("note.xml")
    Dim xmlDoc As XmlDocument = New XmlDocument()
    xmlDoc.Load(file)

    Dim newXMLNode As XmlNode = xmlDoc.SelectSingleNode("/Courses")
    Dim childNode As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "Course", "Course")

    Dim newAttribute As XmlAttribute = xmlDoc.CreateAttribute("name", "sree", "asd")
    childNode.Attributes.Append(newAttribute)
    newXMLNode.AppendChild(childNode)

End Sub
Posted
Updated 11-Apr-18 1:18am
v4
Comments
[no name] 11-Apr-18 7:19am    
Do you want to save the XML file once you added the new node then you are missing the Save operation. The node is already added with your code but in an in-memory object where you loaded the XML, you need to save this file back on disk to see the changes in physical XML file.
Maciej Los 11-Apr-18 11:36am    
My virtual 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900