Implementing program configuration files using XML serialization

Source: Internet
Author: User
Tags file size ini serialization xmlns

Some applications, when exiting, write some setting values to the file so that they can be invoked the next time the program starts, which is called a configuration file. For example, the Windows Minesweeper, which appears at the last shutdown at each startup, is because the minesweeper program writes the current location to the configuration file when it exits.


There are two ways to implement an earlier configuration file. One is the INI file, in the Win32 API is also specifically read and write INI file API functions; the other is the registry, which is also the preferred choice for many programs, in the newer version of the minesweeper is the use of the registry to implement the configuration function. However, both of these methods have their limitations. INI file, simple structure, easy to edit, the capacity of the upper limit (64K). Although the registry is not limited in capacity, reading and writing the registry is a hassle, and it is easy to create redundancy in the registry (the registry is also necessary to remove the program), as well as the possibility of other programs wanting to flush. The main problem is that both the INI file and the registry can only achieve a simple configuration of read and write, it is difficult to read and write similar objects because their entries are parallel to each other, rather than tree-like node structures (the registry can implement a tree structure, but reading and writing is still cumbersome and less secure).

And the use of XML files can be a good remedy for the above deficiencies. The first is file-type, will not read and write the registry, there will be no security worries; second, there is no file size limit, and the XML file is a tree-like structure, which is suitable for the realization of the object's reading and writing. Now the application of XML file is more and more widespread, has become the mainstream, you casually open a program in the directory, rarely do not see the XML file, and the mainstream development software has read and write XML library files. Manipulating an XML file is now a convenient thing to do.

XML file serialization is a branch of XML technology that can serialize an object in a program, such as an instance of a class that you write, into an XML file. He can also deserialize an XML file into an object in a program.

For a good article on XML serialization, I personally feel that the following article is relatively good.

NET object's XML serialization and deserialization

This gives us the idea that we can encapsulate the configuration to read and write in the configuration class that we write, and then serialize it through XML to implement the transformation of the configuration class and XML file.

For example, the following example uses XML serialization to convert between class objects and XML, using C #

The XML schema in the following example describes a simple human resources information that contains most of the formatting of XML, such as nested XML elements, XML elements that have both element values and attribute values.

1. Class hierarchies to be serialized
[XmlRoot ("Humanresource")]public class Humanresource
{
private int m_record = 0;
Private worker[] m_workers = null;
[XmlAttribute (attributename= ' record ')]public int record
{
get {return m_record;}
set {M_record = value;

}

[XmlElement (elementname= "worker")]public worker[] Workers

{
get {return m_workers;}
set {m_workers = value;}

}
}


public class Worker
{

private string m_number = null;

Private informationitem[] M_infoitems = null;
[XmlAttribute ("number")]public string number
{

get {return m_number;}
set {M_number = value;}
}

[XmlElement ("Infoitem")]public informationitem[] Infoitems
{
get {return m_infoitems;}

set {M_infoitems = value;}

}
}


public class Informationitem

{

private string m_name = null;

private string m_value = null;

[XmlAttribute (AttributeName = ' name ')]public string name

{

get {return m_name;}

set {m_name = value;}

}
[Xmltext]public string Value
{

get {return m_value;}

set {m_value = value;}

}
}

2. Serialization of generated XML structures
<?xml version= "1.0"?>

<worker number= "001" >

<infoitem name= "Name" >Michale</infoItem>

<infoitem name= "Sex" >male</infoItem>

<infoitem name= "Age" >25</infoItem>

</worker>
<worker number= "002" >

<infoitem name= "Name" >Surce</infoItem>

<infoitem name= "Sex" >male</infoItem>

<infoitem name= "Age" >28</infoItem>

</worker>

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.