In this example i'm explaining how to Read And Write XML File Data In Asp.Net Using C# VB.NET.I have created a aspx page with three textbox to enter Details, Two buttons to read and write Data in click event of buttons.
I'm using Label control to display XML data after reading it.
HTML SOURCE OF PAGE
Write below mentioned code in click event of respective buttons.
C# CODE
VB.NET CODE
I'm using Label control to display XML data after reading it.
HTML SOURCE OF PAGE
1: <div>
2: Employee Details:
3:
4: Name:
5: <asp:Textbox id="txtName" runat="server"/>
6:
7: Department:
8: <asp:Textbox id="txtDept" runat="server"/>
9:
10: Location:
11: <asp:Textbox id="txtLocation" runat="server"/>
12:
13: <asp:Button ID="btnWrite" runat="server"
14: Text="Write XML File"
15: onclick="btnWrite_Click"/>
16:
17: Read Data :
18: <asp:Button id="btnRead" text="Read XML File"
19: runat="server"
20: onclick="btnRead_Click"/>
21:
22: <asp:label id="lblMsg" runat="server"/>
23:
24: </div>
Write below mentioned code in click event of respective buttons.
C# CODE
01
using
System;
02
using
System.Xml;
03
using
System.Text;
04
05
protected
void
btnRead_Click(
object
sender, EventArgs e)
06
{
07
ReadXmlFile(Server.MapPath(
"EmployeeDetails.xml"
));
08
}
09
protected
void
btnWrite_Click(
object
sender, EventArgs e)
10
{
11
XmlTextWriter xWriter =
new
XmlTextWriter(Server.MapPath(
"EmployeeDetails.xml"
), Encoding.UTF8);
12
xWriter.WriteStartDocument();
13
//Create Parent element
14
xWriter.WriteStartElement(
"EmployeeDetails"
);
15
//Create Child elements
16
xWriter.WriteStartElement(
"Details"
);
17
xWriter.WriteElementString(
"Name"
, txtName.Text);
18
xWriter.WriteElementString(
"Department"
, txtDept.Text);
19
xWriter.WriteElementString(
"Location"
, txtLocation.Text);
20
xWriter.WriteEndElement();
21
22
//End writing top element and XML document
23
xWriter.WriteEndElement();
24
xWriter.WriteEndDocument();
25
xWriter.Close();
26
}
27
28
protected
void
ReadXmlFile(
string
fileName)
29
{
30
string
parentElementName =
""
;
31
string
childElementName =
""
;
32
string
childElementValue =
""
;
33
bool
element =
false
;
34
lblMsg.Text =
""
;
35
36
XmlTextReader xReader =
new
XmlTextReader(fileName);
37
while
(xReader.Read())
38
{
39
if
(xReader.NodeType == XmlNodeType.Element)
40
{
41
if
(element)
42
{
43
parentElementName = parentElementName + childElementName +
"<br>"
;
44
}
45
element =
true
;
46
childElementName = xReader.Name;
47
}
48
else
if
(xReader.NodeType == XmlNodeType.Text | xReader.NodeType == XmlNodeType.CDATA)
49
{
50
element =
false
;
51
childElementValue = xReader.Value;
52
lblMsg.Text = lblMsg.Text +
"<b>"
+ parentElementName +
"<br>"
+ childElementName +
"</b><br>"
+ childElementValue;
53
parentElementName =
""
;
54
childElementName =
""
;
55
}
56
}
57
xReader.Close();
58
}
VB.NET CODE
01
Protected
Sub
btnRead_Click(sender
As
Object
, e
As
EventArgs)
02
ReadXmlFile(Server.MapPath(
"EmployeeDetails.xml"
))
03
End
Sub
04
Protected
Sub
btnWrite_Click(sender
As
Object
, e
As
EventArgs)
05
Dim
xWriter
As
New
XmlTextWriter(Server.MapPath(
"EmployeeDetails.xml"
), Encoding.UTF8)
06
xWriter.WriteStartDocument()
07
'Create Parent element
08
xWriter.WriteStartElement(
"EmployeeDetails"
)
09
'Create Child elements
10
xWriter.WriteStartElement(
"Details"
)
11
xWriter.WriteElementString(
"Name"
, txtName.Text)
12
xWriter.WriteElementString(
"Department"
, txtDept.Text)
13
xWriter.WriteElementString(
"Location"
, txtLocation.Text)
14
xWriter.WriteEndElement()
15
16
'End writing top element and XML document
17
xWriter.WriteEndElement()
18
xWriter.WriteEndDocument()
19
xWriter.Close()
20
End
Sub
21
22
Protected
Sub
ReadXmlFile(fileName
As
String
)
23
Dim
parentElementName
As
String
=
""
24
Dim
childElementName
As
String
=
""
25
Dim
childElementValue
As
String
=
""
26
Dim
element
As
Boolean
=
False
27
lblMsg.Text =
""
28
29
Dim
xReader
As
New
XmlTextReader(fileName)
30
While
xReader.Read()
31
If
xReader.NodeType = XmlNodeType.Element
Then
32
If
element
Then
33
parentElementName = parentElementName & childElementName &
"<br>"
34
End
If
35
element =
True
36
childElementName = xReader.Name
37
ElseIf
xReader.NodeType = XmlNodeType.Text
Or
xReader.NodeType = XmlNodeType.CDATA
Then
38
element =
False
39
childElementValue = xReader.Value
40
lblMsg.Text = lblMsg.Text +
"<b>"
& parentElementName &
"<br>"
& childElementName &
"</b><br>"
& childElementValue
41
parentElementName =
""
42
childElementName =
""
43
End
If
44
End
While
45
xReader.Close()
46
End
Sub
0 comments:
Post a Comment