XML Serialization Data Base

Source: Internet
Author: User
Tags object config net serialization serialization string tostring xmlns
xml| data

The use of XML documents is more and more common, we often put some system settings such as files with config or XML documents stored in the program directory. NET serialization data is a very exciting technology, can easily read a certain format or save as a file. Here's a simple exercise to make some initial understanding of XML serial. (Note: I was doing exercises on the Windows application because of the fear of building Asp.neta engineering problems, a lot of stuff underneath.) In fact, in addition to the path, the program is basically consistent.

First write a class that needs to be serialized, which is a setup file that sets an action permission

Using System;

Namespace Hellosea.WinModule.Account.Configuration
{
<summary>
Summary description of the operatepermissionsettings.
</summary>
public class Operatepermissionsettings
{
Public Operatepermissionsettings ()
{ }

public int systemmanage_login; Allow access to administration pages, permissions for various administrative roles
public int systemmanage_systemsetting; Permission to set system parameters, security, etc.

public int usermanage_enter; Permission to enter User Admin page
public int usermanage_purviewassign; You can assign or reclaim permissions for user roles, licenses, and so on
public int usermanage_userdelete; Can abort a user account license
public int usermanage_useredit; Permission to edit user information
}
}


Serialize the class and save the file:

private void Button1_Click (object sender, System.EventArgs e)
{
Set up an action permission class
Operatepermissionsettings settings = new Operatepermissionsettings ();
Settings. Systemmanage_login = 1;
Settings. systemmanage_systemsetting = 2;
Settings. Usermanage_enter = 3;
Settings. Usermanage_purviewassign = 4;
Settings. Usermanage_useredit = 5;
Settings. Usermanage_userdelete = 6;

File stream
String FilePath = Application.startuppath + "\\OperatePermission.Config";
FileStream fs = new FileStream (FilePath, FileMode.Create);

Serialization of data
XmlSerializer serializer = new XmlSerializer (typeof (Operatepermissionsettings));
Serializer. Serialize (fs, settings);

Close file stream
Fs. Close ();
}


Drag, reading data from a file

private void Button2_Click (object sender, System.EventArgs e)
{
Operatepermissionsettings settings = new Operatepermissionsettings ();

File stream
String FilePath = Application.startuppath + "\\OperatePermission.Config";
FileStream fs = new FileStream (FilePath, FileMode.Open);

Drag rows of data
XmlSerializer serializer = new XmlSerializer (typeof (Operatepermissionsettings));
Settings = (operatepermissionsettings) serializer. Deserialize (FS);

Close file stream
Fs. Close ();

TextBox1.Text = settings. Systemmanage_login.tostring ();
TextBox2.Text = settings. Systemmanage_systemsetting.tostring ();
TextBox3.Text = settings. Usermanage_enter.tostring ();
Textbox4.text = settings. Usermanage_purviewassign.tostring ();
Textbox5.text = settings. Usermanage_useredit.tostring ();
Textbox6.text = settings. Usermanage_userdelete.tostring ();
}


Of course, you need to use two namespaces.
Using System.IO;
Using System.Xml.Serialization;

In addition, the structure of the Operatepermission.config document is as follows:
<?xml version= "1.0"?>
<operatepermissionsettings xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance ">
<SystemManage_Login>1</SystemManage_Login>
<SystemManage_SystemSetting>2</SystemManage_SystemSetting>
<UserManage_Enter>3</UserManage_Enter>
<UserManage_PurviewAssign>4</UserManage_PurviewAssign>
<UserManage_UserDelete>6</UserManage_UserDelete>
<UserManage_UserEdit>5</UserManage_UserEdit>
</OperatePermissionSettings>

This is the simplest serialization, just an example. When you really implement it, it's best to wrap it up in a class and use it to be very readable.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.