【问题标题】:reading xml file in asp.net在asp.net中读取xml文件
【发布时间】:2011-06-09 22:32:49
【问题描述】:

我有一个具有以下结构的 xml 文件:

< rewriteMaps>  
  < rewriteMap name="StaticRewrites" />  
  < add key="/superstar2011" value="/article.aspx?articleid=4014" />  
  < add key="/superstar2012" value="/article.aspx?articleid=4012" />  
  < add key="/superstar2012" value="/article.aspx?articleid=4012" />  
< /rewriteMaps> 

我有一个网格视图,我想用它来绑定键和值。我应该怎么做? 我是使用gridview 的xml 新手。任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net xml gridview


    【解决方案1】:
    XElement x = XElement.Parse("<rewriteMaps><rewriteMap name=\"StaticRewrites\" /><add key=\"/superstar2011\" value=\"/article.aspx?articleid=4014\" /><add key=\"/superstar2012\" value=\"/article.aspx?articleid=4012\" /><add key=\"/superstar2012\" value=\"/article.aspx?articleid=4012\" /></rewriteMaps>");
    
    var r = from i in x.Descendants("add")
                        select new { key = "key", value = "value" };
    
    yourGrid.Datasource = r;
    yourGrid.DataBind();
    

    或者像这个例子:

    DataSet dataSet= new DataSet();
    string filePath = Server.MapPath("your.xml");
    
    dataSet.ReadXml(filePath);                    
    yourGrid.DataSource = dataSet.Tables[0].DefaultView;
    yourGrid.DataBind();
    

    【讨论】:

      【解决方案2】:

      您可以考虑使用 XMLDatasource。

      <asp:xmldatasource id="XmlDataSource1" runat="server" datafile="books.xml" />
      

      然后将其绑定为控件

      <asp:TreeView id="TreeView1" runat="server" datasourceid="XmlDataSource1">
              <databindings>
                <asp:treenodebinding datamember="book" textfield="title"/>
              </databindings>
      </asp:TreeView>
      

      【讨论】:

        【解决方案3】:

        读取 xml 文件并收集数据表或数据集中的数据。然后将 gridview 与这个数据表或数据集绑定。将数据收集到数据集或数据表中后,您可以通过以下 2 行简单地将 gridview 与数据表或数据集绑定:

        GridView1.DataSource=ds;
        GridView1.DataBind();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-14
          • 2015-11-06
          • 2015-12-28
          • 2011-11-23
          • 1970-01-01
          相关资源
          最近更新 更多