Save ADO.NET data processing (serialization and de-serialization, XML files)

Save technical data

Using the document data to be saved

problem

  1. When the object attribute changes, to increase or decrease the number of reads and writes information
  2. Information security is poor

Serialization and de-serialization

Here Insert Picture Description

note:

  1. As long as with can serialize and deserialize
  2. If a data object to be serialized and de-serialization operation, first of all to add properties of this object - serialization logo

Object data stored serialized

        private void btnSavesl_Click(object sender, EventArgs e)
        {
            //首先数据封装
            objStudent student = new objStudent()
            {
                ID = int.Parse(txtID.Text.Trim()),
                Name = txtName.Text.Trim(),
                Age = int.Parse(txtAge.Text.Trim()),
                Birthday = txtBirthday.Text.Trim()
            };
            //【1】创建文件流
            FileStream fs = new FileStream("Student.stu",FileMode.Create);
            //【2】创建二进制格式化器
            BinaryFormatter formatter = new BinaryFormatter();
            //【3】调用这个格式化器的序列化方法
            formatter.Serialize(fs,student);
            fs.Close();
        }

Reading target data deserialized

        private void btnReadersl_Click(object sender, EventArgs e)
        {
            //【1】创建文件流
            FileStream fs = new FileStream("Student.stu", FileMode.Open);
            //【2】创建二进制格式化器
            BinaryFormatter formatter = new BinaryFormatter();
            //【3】调用反序列化方法
            objStudent student=(objStudent)formatter.Deserialize(fs);
            fs.Close();
            txtID.Text = student.ID.ToString();
            txtName.Text = student.Name;
            txtAge.Text = student.Age.ToString();
            txtBirthday.Text = student.Birthday;
        }

Applications

  1. Application configuration information (if large amount of information, the method is more convenient to use the read and write)
  2. When no database, the data can be accessed as a carrier
  3. Transfer WebServerice and other objects of service
  4. Data transfer between modules
    https://www.cnblogs.com/jpfss/p/8397596.html

benefit

  1. Object data access is more convenient, scalability
  2. Stronger data security

Operating XML file

XML concepts

  1. Abbreviation of eXtensible Markup Language, Extensible Markup Language
  2. Is a markup language that can be used to create custom, created by the World Wide Web Consortium (W3C), to overcome the limitations of HTML
  3. From the functional point of view the use of XML is mainly used for storing data, while HTML is mainly used for data display

XML document format requirements

  1. Determined and only the root element
  2. Start and end tags match
  3. Tags properly nested elements
  4. Use attribute values ​​quoted
  5. Properties can not repeat the same element of
    syntax requirements:
  6. Element: <tag> text </ tab>
  7. Processing instruction: <? Xml version = "1.0"?>
  8. Notes: <-! Footnotes ->
  9. 属性:< zhangsan currentMoney=“ZH¥”>200< /zhangsan>

Reading an XML file

Read XML file

  1. Creating a document object
  2. Load xml document
  3. Gets the root node
  4. Traversing node and encapsulated data
        private void btnLoadXML_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load("XMLFile.xml");
            XmlNode root = document.DocumentElement;
            List<Book> books = new List<Book>();
            foreach (XmlNode item in root.ChildNodes)
            {
                if (item.Name=="Book")
                {
                    Book book = new Book();
                    foreach (XmlNode node in item.ChildNodes)
                    {
                        switch (node.Name)
                        {
                            case "Name":
                                book.Name = node.InnerText;
                                break;
                            case "Author":
                                book.Author = node.InnerText;
                                break;
                            case "Price":
                                book.Price = node.InnerText;
                                break;
                            default:
                                break;
                        }
                    }
                    books.Add(book);
                }
            }
            dataGridView1.DataSource = books;
        }

Common object

  1. XmlDocument object represents the entire XML document
  2. XmlNode object represents a single node in the XML file

Common Property / Method

XmlDocument:

  1. DocumentElement: get root
  2. ChildNodes: get inside all child nodes
  3. Load (): read the entire XML structure
  4. Save (): save the entire XML document

XmlNode:

  1. InnerText: acquiring content data between the current node
  2. Name: the name of the current node
  3. ChildNodes: all the child nodes of the current node
  4. AppendChild (): adding a node to the current node byte
Published 134 original articles · won praise 118 · views 10000 +

Guess you like

Origin blog.csdn.net/dust__/article/details/104829409