当前位置:网站首页>C WinForm actual operation XML code, including the demonstration of creating, saving, querying and deleting forms
C WinForm actual operation XML code, including the demonstration of creating, saving, querying and deleting forms
2022-07-24 20:32:00 【Enthusiasts who combine IT technology with enterprise applicati】
1. Add a form , Join in 3 A button control 、1 A text control 、1 individual datagridview Control 、2 Hidden label Control .

2. Code
Execution effect

(1) Button 1 establish xml file , Press the integer in the text box to add to the execution file under the same directory data.xml file ;(2) Button 2 stay datagridview Output in table xml The contents of the document ;(3) Button 3 You can delete rows for options in the table .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
// Define common objects :
public int row;
XmlDocument xmldoc;
XmlElement xmlelement;
DataTable check_dt = new DataTable();
public Form1()
{
InitializeComponent();
dataGridView1.RowHeadersVisible = false;
dataGridView1.DataSource = null;
}
private void button1_Click(object sender, EventArgs e)
{
if (!IsInteger(textBox1.Text))
{
MessageBox.Show(" please enter an integer ", " Tips ");
this.textBox1.Focus();
return;
}
row = Convert.ToInt32(textBox1.Text);
xmldoc = new XmlDocument();
// Join in XML Paragraph of the statement ,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmldeclaration;
xmldeclaration = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmldoc.AppendChild(xmldeclaration);
// Add a root element
xmlelement = xmldoc.CreateElement("", "Books", "");
xmldoc.AppendChild(xmlelement);
// Add another element
for (int i = 1; i < row+1; i++)
{
Thread.Sleep(1);
string x = System.DateTime.Now.Millisecond.ToString();
Random ran=new Random();
int RandKey=ran.Next(10000000,99999999);
int price = ran.Next(100, 200);
XmlNode root = xmldoc.SelectSingleNode("Books");// lookup <Books>
XmlElement dot = xmldoc.CreateElement("Node");// Create a <Node> node
//dot.SetAttribute("ISBN", RandKey.ToString());// Set the node ISBN attribute
dot.SetAttribute("ISBN", RandKey.ToString());// Set the node ISBN attribute
dot.SetAttribute("NAME", " mathematics -" + i.ToString());// Set the node genre attribute
XmlElement sub1 = xmldoc.CreateElement("title");
sub1.InnerText = " Mathematics of tiwangzhi middle school -" + i.ToString();// Set text nodes
dot.AppendChild(sub1);// Add to <Node> In nodes
XmlElement sub2 = xmldoc.CreateElement("price");
sub2.InnerText = price.ToString();
dot.AppendChild(sub2);
root.AppendChild(dot);// Add to <Employees> In nodes
}
// Save the created XML file
xmldoc.Save(Environment.CurrentDirectory + "\\" + "data.xml");
label1.Text = "ok";
}
private void button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]{
new DataColumn("NAME",typeof(string)),
new DataColumn("PRICE",typeof(string))});
String path = Environment.CurrentDirectory + "\\" + "data.xml";
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodes = doc.SelectNodes("/Books/Node");
foreach (XmlNode node in nodes)
{
DataRow r = dt.NewRow();
r[0] = string.Format("{0}", node.ChildNodes[0].InnerText);
r[1] = string.Format("{0}", node.ChildNodes[1].InnerText);
dt.Rows.Add(r);
}
dataGridView1.RowHeadersVisible = false;
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int a = 0;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")
{
{ a++; }
}
}
label2.Text = " Choose "+ a.ToString() + " individual ";
}
private void button3_Click(object sender, EventArgs e)
{
String path = Environment.CurrentDirectory + "\\" + "data.xml";
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodes = doc.SelectNodes("/Books/Node");
foreach (XmlNode node in nodes)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True" && dataGridView1.Rows[i].Cells[2].EditedFormattedValue.ToString() == string.Format("{0}", node.ChildNodes[1].InnerText))
{
node.ParentNode.RemoveChild(node);
}
}
}
doc.Save(Environment.CurrentDirectory + "\\" + "data.xml");
}
public static bool IsInteger(string s)
{
string pattern = @"^\d*$";
return Regex.IsMatch(s, pattern);
}
}
}
边栏推荐
- Fluoronisin peptide nucleic acid oligomer complex | regular active group alkyne, SH thiol alkynyl modified peptide nucleic acid
- Mass modify attribute values in objects in JS
- [advanced data processing technology] data filtering, advanced data filling, initial and advanced data transformation
- Substr and substring function usage in SQL
- [training Day10] tree [interval DP]
- 1. Mx6u-alpha development board (key input experiment)
- [training Day8] tent [mathematics] [DP]
- Software testing interview tips | if you don't receive the offer, I'll wash my hair upside down
- Risk control system, implemented by flink+clickhouse!
- [feature transformation] feature transformation is to ensure small information loss but high-quality prediction results.
猜你喜欢

Leetcode 48 rotating image (horizontal + main diagonal), leetcode 221 maximum square (dynamic programming DP indicates the answer value with ij as the lower right corner), leetcode 240 searching two-d

The difference between map and flatmap in stream
![[training Day9] light tank [dynamic planning]](/img/69/e7a69972a2865408479c7f8c245c1f.png)
[training Day9] light tank [dynamic planning]

Substr and substring function usage in SQL

Safe way -- Analysis of single pipe reverse connection back door

Valdo2021 - vascular space segmentation in vascular disease detection challenge (2)

Login Huawei device in SSH mode

Opengl rendering pipeline

Actual measurement of Qunhui 71000 Gigabit Network
![[training Day9] maze [line segment tree]](/img/56/e8458245fe564821740ab94ece37a4.png)
[training Day9] maze [line segment tree]
随机推荐
Risk control system, implemented by flink+clickhouse!
The difference between map and flatmap in stream
How to learn automated testing? Can you teach yourself?
2022 chemical automation control instrument test question simulation test platform operation
Leetcode 300 longest increasing subsequence (greedy + binary search for the first element subscript smaller than nums[i]), leetcode 200 island number (deep search), leetcode 494 target sum (DFS backtr
[feature construction] construction method of features
Native applets are introduced using vant webapp
Wechat stores build order pages and automatically grab tickets
Pychart tutorial: 5 very useful tips
Make Huawei router into FTP server (realize upload and download function)
Luogu - p1616 crazy herb picking
MySQL stored procedure
[basic data mining technology] KNN simple clustering
API data interface of A-share transaction data
[training Day10] point [enumeration] [bidirectional linked list]
Opengl rendering pipeline
VLAN Technology
Valdo2021 - vascular space segmentation in vascular disease detection challenge (3)
Unity's ugui text component hard row display (improved)
Flink Window&Time 原理