1
我試圖創建一個Web應用程序,它允許你添加&編輯XML文件在VS快遞網絡2013,但我不能爲我的生活弄清楚我做錯了什麼。任何幫助將大大學徒,謝謝!
這裏是我下面的代碼:你DataSet
DS
使用ReadXml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Windows;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace P.Marina
{
public partial class SlipBooking : System.Web.UI.Page
{
DataSet DS = new DataSet();
DataView dv = new DataView();
enum DBNum : int { Customer };
protected void PageLoad(object sender, EventArgs e)
{
LoadDatabase();
}
void LoadDatabase()
{
DS.ReadXml(Server.MapPath("Customer.xml"), XmlReadMode.InferSchema);
DS.Tables[(int)DBNum.Customer].PrimaryKey = new DataColumn[] {
DS.Tables[0].Columns["ID"] };
CustomerRecordList();
}
public void CustomerRecordList()
{
DataView cv = DS.Tables[(int)DBNum.Customer].DefaultView;
cv.Sort = "Name";
CustomerList.DataTextField = "Name";
CustomerList.DataValueField = "ID";
CustomerList.DataSource = cv;
if (!IsPostBack)
{
CustomerList.DataBind();
}
}
// customer dropdownlist
protected void btnCustomerRecord_Click(object sender, EventArgs e)
{
DataRow DR;
DR = DS.Tables[(int)DBNum.Customer].Rows.Find(CustomerList.SelectedValue);
string ID = DR[0].ToString();
string Name = DR[1].ToString();
string Address = DR[2].ToString();
string Email = DR[3].ToString();
Label27.Text = ID;
Label3.Text = Name;
Label4.Text = Address;
Label5.Text = Email;
TextBox1.Text = Name;
TextBox2.Text = Address;
TextBox4.Text = Email;
}
// Following code is for editing the customer informnation.
protected void Button7_Click(object sender, EventArgs e)
{
DataRow CR;
CR = DS.Tables[(int)DBNum.Customer].Rows.Find(CustomerList.SelectedValue);
if (TextBox1.Text == Name.Text)
{
}
else
{
CR[1] = TextBox1.Text;
}
if (TextBox2.Text == Address.Text)
{
}
else
{
CR[2] = TextBox2.Text;
}
if
(TextBox4.Text == Email.Text)
{
}
else
{
CR[3] = TextBox4.Text;
}
DS.AcceptChanges();
var editCustomerFileLocation = File.Create(Server.MapPath("Customer.xml"));
DS.Tables[(int)DBNum.Customer].WriteXml(editCustomerFileLocation);
editCustomerFileLocation.Close();
DS.Clear();
LoadDatabase();
CustomerList.DataBind();
}
public void Button8_Click(object sender, EventArgs e)
{
DataRow NewRow = DS.Tables[(int)DBNum.Customer].NewRow();
NewRow[0] = DS.Tables[(int)DBNum.Customer].Rows.Count + 1;
int index = DS.Tables[(int)DBNum.Customer].Rows.Count + 1;
if (TextBox5.Text == "")
{ }
else
{
NewRow[1] = TextBox5.Text;
}
if (TextBox6.Text == "")
{ }
else
{
NewRow[2] = TextBox6.Text;
}
if (TextBox7.Text == "")
{ }
else
{
NewRow[3] = TextBox7.Text;
}
/*NewRow[4] = "";*/
DS.Tables[(int)DBNum.Customer].Rows.InsertAt(NewRow, index);
var fileLocation = File.Create(Server.MapPath("Customer.xml"));
DS.Tables[(int)DBNum.Customer].WriteXml(fileLocation);
fileLocation.Close();
DS.Clear();
LoadDatabase();
CustomerList.DataBind();
}
}
}
}
請原諒我的幼稚與我非常感謝你這麼快回答我的問題。如何適當地使用DS.Tables.Add()?它是基於我試圖添加/更新的信息的數量嗎? – RussellB
@RussellB當你調用'DS.ReadXml()'時,你正在向數據集添加數據。調試行後查看錶的數量。 – user3185569