Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add the xml element node when you are entering the values from controls like textbox,combobox etc.
when you select the combobox value that time need to update /delete in/from the existing xml file data.


any help would be appreciated..Thanks

What I have tried:

VB
Private Sub btnPgAdd_Click(sender As Object, e As EventArgs) Handles btnPgAdd.Click
        'validate txtboxes later
        xmldoc1 = XDocument.Load(XMLFile)
        Dim xmls As New XElement("Page", New XElement("Title", cmbtitle.Text), New XAttribute("id", cmbtitle.Text), New XElement("MenuDescription", New XElement("ShortDesc", txtstdesc.Text), New XElement("LongDesc", txtlongdesc.Text), New XElement("Link", New XAttribute("Path", txtlink.Text), txtlink.Text)))
        xmldoc1.Element("AddPage").Add(xmls)
        xmldoc1.Save(XMLFile)
        Dim i As Integer
        For i = 0 To dtgrdPage.RowCount - 1
            If dtgrdPage.Rows(i).Cells.Item(1).Value Is Nothing Then
                With dtgrdPage
                    .Rows(i).Cells("ShortDesc").Value = txtstdesc.Text.ToString()
                    .Rows(i).Cells("Link").Value = txtlink.Text.ToString()
                End With
            End If
        Next
        cmbtitle.Text = ""
        txtstdesc.Text = ""
        txtlongdesc.Text = ""
        txtlink.Text = ""
    End Sub

this is on update button
VB
Private Sub btnUpdate_Click(sender As Object, e As EventArgs)
        doc1.Load(XMLFile)
        Dim TitleID As String = cmbtitle.Text
        Dim ShortDesc As String = txtstdesc.Text
        Dim LongDesc As String = txtlongdesc.Text
        Dim Link As String = txtlink.Text
        Dim xmlnode1 As XmlNode = doc1.SelectSingleNode("Page")
        ' If xmlnode1 IsNot Nothing Then
        xmlnode1.ChildNodes(0).InnerText = TitleID
        xmlnode1.ChildNodes(0).InnerText = ShortDesc
        xmlnode1.ChildNodes(0).InnerText = LongDesc
        xmlnode1.ChildNodes(0).InnerText = Link
        'End If
        ' xmldoc1.Element("Page").Add(xmlnode1)
        doc1.Save(XMLFile)
    End Sub
Posted
Updated 16-Oct-17 8:39am
v2

1 solution

There are 2 well known classes you can use to operate on xml files:
XmlDocument Class (System.Xml)[^]
XDocument Class (System.Xml.Linq)[^]

Good luck!
 
Share this answer
 

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