xml

      DataSet1  添加一个 DeviceTable

双击DeviceTable 添加一查找方法

 public partial class DataSet1 {
        partial class DeviceTableDataTable
        {
            public DeviceTableRow FindbyKey(string key)
            {
                DeviceTableRow DeviceRow = null;
                foreach (DeviceTableRow row in this.Rows)
                {
                    if (row.key == key)
                    {
                        DeviceRow = row;
                        break;
                    }
                }
                return DeviceRow;
            }

        }
    }

 

private void btnAdd_Click(object sender, EventArgs e)
        {
            DataSet1 ds = this.GetDevices();
            for (int i = 0; i < 10; i++)
            {
                DataSet1.DeviceTableRow row = ds.DeviceTable.NewDeviceTableRow();
                row.id = i+1;
                row.key = i.ToString();
                row.datatime = DateTime.Now;
                ds.DeviceTable.Rows.Add(row);
            }
           
            ds.AcceptChanges();
            ds.WriteXml(Path.Combine(Environment.CurrentDirectory, "text2.xml"));
           
        }

 

private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataSet1 ds = this.GetDevices();           
            //this.dataGridView1.CurrentCell.Value.ToString();
            //String str = this.dataGridView1.SelectedCells[0].Value.ToString();
            int id = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells["id"].Value.ToString());
            DataSet1.DeviceTableRow row = ds.DeviceTable.FindByid(id);
         //   DataSet1.DeviceTableRow row2 = ds.DeviceTable.FindbyKey("10up");
            row.key = "10up";
            row.datatime = DateTime.Now;
         
            ds.AcceptChanges();
            ds.WriteXml(Path.Combine(Environment.CurrentDirectory, "text2.xml"));
            BindGridView();
        }

 

 private void btnDelete_Click(object sender, EventArgs e)
        {
            DataSet1 ds = this.GetDevices();
            int id = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells["id"].Value.ToString());
            DataSet1.DeviceTableRow row = ds.DeviceTable.FindByid(id);
            ds.DeviceTable.RemoveDeviceTableRow(row);

            ds.AcceptChanges();
            ds.WriteXml(Path.Combine(Environment.CurrentDirectory, "text2.xml"));
            BindGridView();
        }

 

public DataSet1 GetDevices()
        {
            DataSet1 ds = new DataSet1();
            ds.ReadXml(Path.Combine(Environment.CurrentDirectory, "text2.xml"));           
            return ds;

        }

 

 

private void WriteXML()
        {
            string pro_path = Environment.CurrentDirectory;
            string xmlPath = Path.Combine(pro_path, "text.xml");
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(xmlPath, false))
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                // provide the XML declaration
                xmlWriterSettings.OmitXmlDeclaration = false;
                // write attributes on the new line
                // xmlWriterSettings.NewLineOnAttributes = true;
                // indent elements
                xmlWriterSettings.Indent = true;
                XmlWriter writer = XmlWriter.Create(sw, xmlWriterSettings);
                try
                {
                    writer.WriteStartDocument(true);
                    writer.WriteRaw(Environment.NewLine);
                    //writer.WriteProcessingInstruction("xml-stylesheet", "type=/"text/xsl/" href=/"DevicesStylesheet.xsl/"");
                    writer.WriteStartElement("DeviceSet");
                    writer.WriteAttributeString("xsd", "noNamespaceSchemaLocation",
                        "http://www.w3.org/2001/XMLSchema-instance", "DevicesSchema.xsd");

                    for (int i = 0; i < 10; i++)
                    {
                        writer.WriteStartElement("Device");
                        writer.WriteAttributeString("key", i.ToString());
                        writer.WriteElementString("id", (i + 1).ToString());
                        writer.WriteElementString("datetime", DateTime.Now.ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }
                finally
                {
                    writer.Close();
                    sw.Close();
                }
                               
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值