My xml file is like:-
<?xml version="1.0" encoding="utf-8"?>
<Student>
<Name id="1">
<FirstName>Ajay</FirstName>
<LastName>Gupta</LastName>
</Name>
<Name id="2">
<FirstName>Sandeep</FirstName>
<LastName>Yadav</LastName>
</Name>
<Name id="3">
<FirstName>Namrata</FirstName>
<LastName>Singh</LastName>
</Name>
<Name id="4">
<FirstName>Amy</FirstName>
<LastName>Jackson</LastName>
</Name>
<Name id="5">
<FirstName>Lalit</FirstName>
<LastName>Kumar</LastName>
</Name>
<Name id="6">
<FirstName>Ranjit</FirstName>
<LastName>Winy</LastName>
</Name>
</Student>
***************************
For Reading
Protected Sub ShowRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowRecord.Click
Dim xd As New XmlDocument()
xd.Load(Server.MapPath("database/XMLFile.xml"))
Dim node = xd.SelectSingleNode("/Student/Name[@id='" + Txtid2.Text + "']")
If node IsNot Nothing Then
TxtFN2.Text = node.ChildNodes(0).InnerText
TxtLN2.Text = node.ChildNodes(1).InnerText
div1.Visible = True
Else
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "clientscript", "alert('Record Not Found.')", True)
End If
End Sub
For Adding :-
Protected Sub AddRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRecord.Click
Dim newStudent As String = "<Name id=""" + Txtid1.Text + """>" & Environment.NewLine & " " & _
" <FirstName>" + TxtFN1.Text + "</FirstName>" & Environment.NewLine & " " & _
" <LastName>" + TxtLN1.Text + "</LastName>" & Environment.NewLine & " " & _
"</Name>"
Dim xd As New XmlDocument()
xd.Load(Server.MapPath("database/XMLFile.xml"))
Dim DocFrag As XmlDocumentFragment = xd.CreateDocumentFragment()
DocFrag.InnerXml = newStudent
Dim Root As XmlNode = xd.DocumentElement
Root.AppendChild(DocFrag)
xd.Save(Server.MapPath("database/XMLFile.xml"))
xd = New XmlDocument()
xd.Load(Server.MapPath("database/XMLFile.xml"))
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "clientScript", "alert('Record Successfully Saved')", True)
End Sub
<?xml version="1.0" encoding="utf-8"?>
<Student>
<Name id="1">
<FirstName>Ajay</FirstName>
<LastName>Gupta</LastName>
</Name>
<Name id="2">
<FirstName>Sandeep</FirstName>
<LastName>Yadav</LastName>
</Name>
<Name id="3">
<FirstName>Namrata</FirstName>
<LastName>Singh</LastName>
</Name>
<Name id="4">
<FirstName>Amy</FirstName>
<LastName>Jackson</LastName>
</Name>
<Name id="5">
<FirstName>Lalit</FirstName>
<LastName>Kumar</LastName>
</Name>
<Name id="6">
<FirstName>Ranjit</FirstName>
<LastName>Winy</LastName>
</Name>
</Student>
***************************
For Reading
Protected Sub ShowRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowRecord.Click
Dim xd As New XmlDocument()
xd.Load(Server.MapPath("database/XMLFile.xml"))
Dim node = xd.SelectSingleNode("/Student/Name[@id='" + Txtid2.Text + "']")
If node IsNot Nothing Then
TxtFN2.Text = node.ChildNodes(0).InnerText
TxtLN2.Text = node.ChildNodes(1).InnerText
div1.Visible = True
Else
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "clientscript", "alert('Record Not Found.')", True)
End If
End Sub
For Adding :-
Protected Sub AddRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRecord.Click
Dim newStudent As String = "<Name id=""" + Txtid1.Text + """>" & Environment.NewLine & " " & _
" <FirstName>" + TxtFN1.Text + "</FirstName>" & Environment.NewLine & " " & _
" <LastName>" + TxtLN1.Text + "</LastName>" & Environment.NewLine & " " & _
"</Name>"
Dim xd As New XmlDocument()
xd.Load(Server.MapPath("database/XMLFile.xml"))
Dim DocFrag As XmlDocumentFragment = xd.CreateDocumentFragment()
DocFrag.InnerXml = newStudent
Dim Root As XmlNode = xd.DocumentElement
Root.AppendChild(DocFrag)
xd.Save(Server.MapPath("database/XMLFile.xml"))
xd = New XmlDocument()
xd.Load(Server.MapPath("database/XMLFile.xml"))
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "clientScript", "alert('Record Successfully Saved')", True)
End Sub