How to display xml data in asp.net
-
24-06-2021 - |
Question
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<entry>
<comment Name="xxx" Location="India" Email="xxx@email.com" Gender="Male" />
<comment Name="yyy" Location="usa" Email="yyy@email.com" Gender="Male" />
<comment Name="zzz" Location="uae" Email="zzz@email.com" Gender="Male" />
<comment Name="abc" Location="china" Email="abc@email.com" Gender="Male" />
</entry>
How do I display only the comment tags' data (e.g. name,location,email,gender) in ASP.NET? Please help. Thank you.
Solution
Output
Code Behind
using (DataSet ds = new DataSet())
{
ds.ReadXml(MapPath("XMLFile1.xml"));
grd.DataSource = ds;
grd.DataBind();
}
Mark Up
<asp:GridView ID="grd" runat="server"></asp:GridView>
Add an Xml File in the Project
OTHER TIPS
You can use an Xml Data Source with Tree view, Repeater control as
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml" XPath="entry" >
</asp:XmlDataSource>
//Repeater
<asp:Repeater ID="Repeater1"
runat="server"
DataSourceID="XmlDataSource1">
<ItemTemplate>
<h2>Entry</h2>
<table>
<tr>
<td><%#XPath("comment[1]/@Name")%></td>
<td><%#XPath("comment[1]/@Location")%></td>
<td><%#XPath("comment[1]/@Email")%></td>
<td><%#XPath("comment[1]/@Gender")%></td>
</tr>
<tr>
<td><%#XPath("comment[2]/@Name")%></td>
<td><%#XPath("comment[2]/@Location")%></td>
<td><%#XPath("comment[2]/@Email")%></td>
<td><%#XPath("comment[2]/@Gender")%></td>
</tr>
<td><%#XPath("comment[3]/@Name")%></td>
<td><%#XPath("comment[3]/@Location")%></td>
<td><%#XPath("comment[3]/@Email")%></td>
<td><%#XPath("comment[3]/@Gender")%></td>
</tr>
<tr>
<td><%#XPath("comment[4]/@Name")%></td>
<td><%#XPath("comment[4]/@Location")%></td>
<td><%#XPath("comment[4]/@Email")%></td>
<td><%#XPath("comment[4]/@Gender")%></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
// Output
Entry
xxx India xxx@email.com Male
yyy usa yyy@email.com Male
zzz uae zzz@email.com Male
abc china abc@email.com Male
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow