Article Directory
Save technical data
Using the document data to be saved
problem
- When the object attribute changes, to increase or decrease the number of reads and writes information
- Information security is poor
Serialization and de-serialization
note:
- As long as with can serialize and deserialize
- 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
- Application configuration information (if large amount of information, the method is more convenient to use the read and write)
- When no database, the data can be accessed as a carrier
- Transfer WebServerice and other objects of service
- Data transfer between modules
https://www.cnblogs.com/jpfss/p/8397596.html
benefit
- Object data access is more convenient, scalability
- Stronger data security
Operating XML file
XML concepts
- Abbreviation of eXtensible Markup Language, Extensible Markup Language
- 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
- 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
- Determined and only the root element
- Start and end tags match
- Tags properly nested elements
- Use attribute values quoted
- Properties can not repeat the same element of
syntax requirements: - Element: <tag> text </ tab>
- Processing instruction: <? Xml version = "1.0"?>
- Notes: <-! Footnotes ->
- 属性:< zhangsan currentMoney=“ZH¥”>200< /zhangsan>
Reading an XML file
Read XML file
- Creating a document object
- Load xml document
- Gets the root node
- 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
- XmlDocument object represents the entire XML document
- XmlNode object represents a single node in the XML file
Common Property / Method
XmlDocument:
- DocumentElement: get root
- ChildNodes: get inside all child nodes
- Load (): read the entire XML structure
- Save (): save the entire XML document
XmlNode:
- InnerText: acquiring content data between the current node
- Name: the name of the current node
- ChildNodes: all the child nodes of the current node
- AppendChild (): adding a node to the current node byte