简体   繁体   中英

how to split the field from datatable

I have an url which contain data in xml format. 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". 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.

Try to implement with this example:

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

Issue 2: To get a specific column from 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
                  }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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