C#/.NET数据存储到XML

    有时候,在存储数据的时候要求存储成XML格式,我们用代码一行一行写的话比较繁琐,我们可以先把数据放到IList中,然后把IList中的数据放到DataSet中,DataSet有个方法WriteXml()把DataSet中的数据存储到XML中,看下面代码:

存储到xml:
internal void Save()
        {
            System.Data.DataSet ds = ChangeToDataSet(IList);

            ds.ReadXmlSchema(getStrByResFileName("PosLog.xsd")); //格式,这步可以不要
            string path = "要存的路径";
            ds.WriteXml(path);
        }

//IList转化为DataSet:
private System.Data.DataSet ChangeToDataSet(IList list)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            System.Data.DataTable table = new System.Data.DataTable("CouponInfo");
            ds.Tables.Add(table);
            table.Columns.Add("ID", typeof(Guid));
            table.Columns.Add("PromotionID", typeof(string));
            table.Columns.Add("Header", typeof(string));
            table.Columns.Add("Discription", typeof(string));
            table.Columns.Add("ExtraPrintInfo", typeof(string));
            table.Columns.Add("Piclist", typeof(string));
            table.Columns.Add("ExpiryDate", typeof(DateTime));
            table.Columns.Add("Template", typeof(string));
            table.Columns.Add("Barcode", typeof(string));

            foreach (CouponInfo cp in list)
            {
                System.Data.DataRow row = table.NewRow();
                row[0] = cp.ID;
                row[1] = cp.PromotionID;
                row[2] = cp.Header;
                row[3] = cp.Discription;
                row[4] = cp.ExtraPrintInfo;
                row[5] = cp.Piclist;
                row[6] = cp.ExpiryDate;
                row[7] = cp.Template;
                row[8] = cp.Barcode;
                ds.Tables["CouponInfo"].Rows.Add(row);
            }
            return ds;
        }



读取XML:

internal void Fatch()
        {
            System.Data.DataSet ds = new System.Data.DataSet();

            ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "CouponList.xml");
            SetCuoponList(ds);
         }

private void SetCuoponList(System.Data.DataSet ds)
        {
            if (ds.Tables.Count != 0)
            {
                foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                {
                    CouponInfo coupon = new CouponInfo();
                    coupon.SetID(new Guid(row["ID"].ToString()));
                    coupon.PromotionID = row["PromotionID"].ToString();
                    coupon.Header = row["Header"].ToString();
                    coupon.Discription = row["Discription"].ToString();
                    coupon.ExtraPrintInfo = row["ExtraPrintInfo"].ToString();
                    coupon.Piclist = row["Piclist"].ToString();
                    coupon.ExpiryDate = DateTime.Parse(row["ExpiryDate"].ToString());
                    coupon.Template = row["Template"].ToString();
                    coupon.Barcode = row["Barcode"].ToString();
                    this.InnerList.Add(coupon);
                }
            }
            else
            {
                //throw new Exception("CouponList File is Empty");
            }
        }

你可能感兴趣的:(C#数据保存到XML)