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