使用C#应用程序添加&;编辑XML文件
我正在尝试创建一个web应用程序,允许您在VS Express web 2013中添加和编辑XML文件,但我一生都无法找出我做错了什么。任何帮助都是徒弟,谢谢 下面是我的代码:使用C#应用程序添加&;编辑XML文件,c#,xml,visual-studio-2013,C#,Xml,Visual Studio 2013,我正在尝试创建一个web应用程序,允许您在VS Express web 2013中添加和编辑XML文件,但我一生都无法找出我做错了什么。任何帮助都是徒弟,谢谢 下面是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls
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();
}
}
}
}
您正在使用
ReadXml
向数据集添加数据。但它似乎是空的,因为DS.Tables[0]
抛出索引超出范围异常
您需要确保数据填写正确:
void LoadDatabase()
{
DS.ReadXml(Server.MapPath("Customer.xml"), XmlReadMode.InferSchema);
// check the number of tables here
int count = ds.Tables.Count;
System.Diagnostics.Debugger.Break();
DS.Tables[(int)DBNum.Customer].PrimaryKey = new DataColumn[] {
DS.Tables[0].Columns["ID"] };
CustomerRecordList();
}
请原谅我的天真&我非常感谢你这么快就回答了我的问题。如何正确使用DS.Tables.Add()?它是基于我试图添加/更新的信息吗?@RussellB当您调用DS.ReadXml()
将数据添加到数据集时。调试后的行并查看表的数量。