简体   繁体   English

如何从数据表中拆分字段

[英]how to split the field from datatable

I have an url which contain data in xml format. 我有一个包含xml格式数据的网址。 Please see my code:- 请看我的代码:

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();
   }
}

this gives me the data in xls file but i want to add a new column name "Event" & need specific column from xml like i want "name","address". 这给了我xls文件中的数据,但是我想添加一个新的列名“ Event”,并且需要xml中的特定列,例如我想要“ name”,“ address”。 how it would be possible? 怎么可能? please help me on this... 请帮我...

After constructing data in data table. 在数据表中构造数据之后。 Add those new columns in data table like following - 将这些新列添加到数据表中,如下所示-

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

reference : http://msdn.microsoft.com/en-us/library/hfx3s9wd(v=vs.110).aspx and then put required data from the existing columns via for loop or existing queries. 参考: http : //msdn.microsoft.com/zh-cn/library/hfx3s9wd(v=vs.110).aspx ,然后通过for循环或现有查询从现有列中放入所需数据。

Try to implement with this example: 尝试通过以下示例实现:

Issue 1:To add new column name "Event" 问题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();

Issue 2: To get a specific column from xml 问题2:从xml获取特定列

For unique column: 对于唯一列:

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

If there are multiple columns with same name: 如果有多个具有相同名称的列:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM