如何在asp.net中显示xml数据

时间:2012-08-05 06:05:48

标签: asp.net xml data-representation

这是我的xml文件:

<?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>

如何在ASP.NET中仅显示评论标签的数据(例如姓名,位置,电子邮件,性别)?请帮忙。谢谢。

2 个答案:

答案 0 :(得分:1)

输出

enter image description here


代码背后

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>

在项目中添加Xml文件

答案 1 :(得分:1)

您可以将Xml数据源与树视图一起使用,将Repeater控件用作

<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