【问题标题】:How to display xml data in asp.net如何在asp.net中显示xml数据
【发布时间】:2012-08-05 06:05:47
【问题描述】:

这是我的 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 中仅显示评论标签的数据(例如姓名、位置、电子邮件、性别)?请帮忙。谢谢。

【问题讨论】:

  • 您还应该投票选出最能满足您需求的答案

标签: asp.net xml data-representation


【解决方案1】:

输出


代码隐藏

using (DataSet ds = new DataSet())
{
    ds.ReadXml(MapPath("XMLFile1.xml"));
    grd.DataSource = ds;
    grd.DataBind();
}

标记

<asp:GridView ID="grd" runat="server"></asp:GridView>

在项目中添加一个 Xml 文件

【讨论】:

    【解决方案2】:

    您可以将 Xml 数据源与 Tree 视图、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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多