Home Database Mysql Tutorial 序列化与反序列化(2)

序列化与反序列化(2)

Jun 07, 2016 pm 03:44 PM
xml binary object Serialization automatic Convert

自动对象序列化是转换XML和二进制对象的最简单方法。需要特别说明的是,自动对象序列化仅对公有数据类型和公有数据成员有效。 public class Person{ public string FirstName { get; set; } public char MiddleInitial { get; set; } public string LastName

自动对象序列化是转换XML和二进制对象的最简单方法。需要特别说明的是,自动对象序列化仅对公有数据类型和公有数据成员有效。
public class Person
{
    public string FirstName { get; set; }
    public char MiddleInitial { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public double HighschoolGPA { get; set; }
    public Address Address { get; set; }
    //to be XML serialized, the type must have
    //a parameterless constructor
    public Person() { }
    public Person(string firstName, char middleInitial, string lastName,
    DateTime birthDate, double highSchoolGpa, Address address)
    {
        this.FirstName = firstName;
        this.MiddleInitial = middleInitial;
        this.LastName = lastName;
        this.BirthDate = birthDate;
        this.HighschoolGPA = highSchoolGpa;
        this.Address = address;
    }

    public override string ToString()
    {
        return FirstName + " " + MiddleInitial + ". " + LastName + ", DOB:" + BirthDate.ToShortDateString() + ", GPA: " + this.HighschoolGPA + Environment.NewLine + Address.ToString();
    }
}
//sorry, don’t feel like listing out 50 states :)
public enum StateAbbreviation { RI, VA, SC, CA, TX, UT, WA };
public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public StateAbbreviation State { get; set; }
    public string ZipCode { get; set; }
    public Address() { }
    public Address(string addressLine1, string addressLine2,
    string city, StateAbbreviation state, string zipCode)
    {
        this.AddressLine1 = addressLine1;
        this.AddressLine2 = addressLine2;
        this.City = city;
        this.State = state;
        this.ZipCode = zipCode;
    }
    public override string ToString()
    {
        return AddressLine1 + Environment.NewLine + AddressLine2 + Environment.NewLine + City + ", " + State + " " + ZipCode;
    }
}
Copy after login


使用简单的代码进行序列化(然后反序列化)。

Person person = new Person("John", 'Q', "Public",
                           new DateTime(1776, 7, 4), 3.5,
                           new Address("1234 Cherry Lane", null, "Smalltown",
                                       StateAbbreviation.VA, "10000"));
Console.WriteLine("Before serialize:" + Environment.NewLine +
                  person.ToString());
XmlSerializer serializer = new XmlSerializer(typeof (Person));
//for demo purposes, just serialize to a string
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
    //the actual serialization
    serializer.Serialize(sw, person);
    Console.WriteLine(Environment.NewLine + "XML:" + Environment.NewLine +
                      sb.ToString() + Environment.NewLine);
}
using (StringReader sr = new StringReader(sb.ToString()))
{
    //deserialize from text back into binary
    Person newPerson = serializer.Deserialize(sr) as Person;
    Console.WriteLine("After deserialize:" + Environment.NewLine +
                      newPerson.ToString());
}
Copy after login


输出为:

Before serialize:

John Q. Public, DOB:7/4/1776, GPA: 3.5

1234 Cherry Lane

Smalltown, VA 10000

XML:

    John

    81

    Public

    1776-07-04T00:00:00

    3.5

   

        1234 Cherry Lane

        Smalltown

        VA

        10000

   

After deserialize:

John Q. Public, DOB:7/4/1776, GPA: 3.5

1234 Cherry Lane

Smalltown, VA 10000

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone Mar 21, 2024 pm 01:21 PM

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

Automount drives on Linux Automount drives on Linux Mar 20, 2024 am 11:30 AM

If you are using a Linux operating system and want the system to automatically mount the drive on boot, you can do this by adding the device's unique identifier (UID) and mount point path to the fstab configuration file. fstab is a file system table file located in the /etc directory. It contains information about the file systems that need to be mounted when the system starts. By editing the fstab file, you can ensure that the required drives are loaded correctly every time the system starts, thus ensuring stable system operation. Automatically mounting drivers can be conveniently used in a variety of situations. For example, I plan to back up my system to an external storage device. To achieve automation, ensure that the device remains connected to the system, even at startup. Likewise, many applications will directly

How to convert full-width English letters into half-width letters How to convert full-width English letters into half-width letters Mar 25, 2024 pm 02:45 PM

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

Quickly learn about ASCII value conversion in PHP Quickly learn about ASCII value conversion in PHP Mar 28, 2024 pm 06:42 PM

ASCII value conversion in PHP is a problem often encountered in programming. ASCII (American Standard Code for Information Interchange) is a standard encoding system for converting characters into numbers. In PHP, we often need to convert between characters and numbers through ASCII code. This article will introduce how to convert ASCII values ​​in PHP and give specific code examples. 1. Change the characters

See all articles