【问题标题】:how to split the field from datatable如何从数据表中拆分字段
【发布时间】:2014-03-24 13:34:42
【问题描述】:

我有一个 url,其中包含 xml 格式的数据。请看我的代码:-

public partial class WebForm1 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!IsPostBack)
      {
         GridData();
      }
   }

   protected void GridData()
   {
      string url = "http://s3.amazonaws.com/webassets.ticketmob.com/feeds/31squares/tunestub-XML.xml";

      XmlDocument doc = new XmlDocument();
      DataSet ds = new DataSet();
      DataTable dt = new DataTable();
      ds.ReadXml(url);
      dt = ds.Tables[1];

      grid.DataSource = ds.Tables[1];
      grid.DataBind();
   }
   public override void VerifyRenderingInServerForm(Control control)
   {
   }
   protected void btnExport_Click(object sender, EventArgs e)
   {
      Response.ClearContent();
      Response.Buffer = true;
      Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ExportVenue.xls"));
      Response.ContentType = "application/ms-excel";
      StringWriter sw = new StringWriter();
      HtmlTextWriter htw = new HtmlTextWriter(sw);

      GridData();

      for (int i = 0; i < grid.HeaderRow.Cells.Count; i++)
      {
         grid.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
      }
      grid.RenderControl(htw);
      Response.Write(sw.ToString());
      Response.End();
   }
}

这给了我 xls 文件中的数据,但我想添加一个新的列名“事件”并且需要来自 xml 的特定列,就像我想要“名称”、“地址”一样。 怎么可能?请帮我解决这个问题...

【问题讨论】:

    标签: c# asp.net xml datatable dataset


    【解决方案1】:

    在数据表中构造数据后。在数据表中添加这些新列,如下所示 -

    DataColumn nameCol = datatable.Columns.Add("Name", typeof(string));
    nameCol.AllowDBNull = false;
    

    参考:http://msdn.microsoft.com/en-us/library/hfx3s9wd(v=vs.110).aspx 然后通过 for 循环或现有查询从现有列中放入所需数据。

    【讨论】:

      【解决方案2】:

      尝试用这个例子来实现:

      问题1:添加新的列名“事件”

      string path=Application.StartupPath + @"\Test.xml";
      XmlWriter writer = XmlWriter.Create(path);
      XmlDocument doc = new XmlDocument();
      doc.Load(path);
      writer.WriteStartElement("Event");
      writer.WriteString("Event Decription");
      writer.WriteEndElement();
      writer.WriteEndElement();
      writer.WriteEndDocument();
      writer.Close();
      

      问题 2:从 xml 中获取特定列

      对于唯一列:

      XmlNodeList elemnt1 = doc.GetElementsByTagName("name");
      XmlNodeList elemnt2 = doc.GetElementsByTagName("address");
      

      如果有多列同名:

      XmlNodeList elem1 = doc.GetElementsByTagName("name");
       for (int j = 0; j < elem1 .Count; j++)
           {
                if (elem1[j].InnerText == "name")
                       {
                          //want u want to do
                        }
            }
      

      【讨论】:

        猜你喜欢
        • 2021-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        相关资源
        最近更新 更多