如何从数据表中分离字段

本文关键字:分离 字段 数据表 | 更新日期: 2023-09-27 17:52:51

我有一个包含xml格式数据的url。请参阅我的代码:-

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喜欢我想要的"名称","地址"。这怎么可能呢?

如何从数据表中分离字段

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

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

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

尝试用这个例子实现:

第1期:添加新的列名"Event"

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