使用C#应用程序添加&编辑XML文件

时间:2016-07-27 04:45:47

标签: c# xml visual-studio-2013

enter image description here

我正在尝试创建一个允许您添加&在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.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();
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您正在使用DataSetDS 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();
}