[英]how to split the field from datatable
我有一個包含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文件中的數據,但是我想添加一個新的列名“ Event”,並且需要xml中的特定列,例如我想要“ name”,“ address”。 怎么可能? 請幫我...
在數據表中構造數據之后。 將這些新列添加到數據表中,如下所示-
DataColumn nameCol = datatable.Columns.Add("Name", typeof(string));
nameCol.AllowDBNull = false;
參考: http : //msdn.microsoft.com/zh-cn/library/hfx3s9wd(v=vs.110).aspx ,然后通過for循環或現有查詢從現有列中放入所需數據。
嘗試通過以下示例實現:
問題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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.