Xmlserializer provides another method, which enables you to serialize your own object string columns and deserials into XML. the stringized data allows you to access the data as a processing file. At the same time, you can skip uninteresting elements. in this article, I will show you how to use the xmlserializer class and how to add modularity to your class to control the stringized process.
Xmlserializer
Xmlserializer type exists in system. XML. serialization name: system. XML. in DLL, it provides the string-column service in a highly loosely coupled manner. your class does not need to inherit Special Base classes, and they do not need to implement any special interfaces. on the contrary, you only need to add custom semantics to your class or the public domain of these classes, as well as their/their adequacy. xmlserializer uses reverse ing to get these features and uses them to map your Class and Class Members to XML elements and features.
1. Map XML to objects
A. Theater. xml
Warner
(888) 665-2222
R
16:15:00
19:05:00
21:40:00
PG-13
16:00:00
19:00:00
21:40:00
Next, we define a theater (cinema) class, which contains the attention ing used by xmlserializer.
Using system;
Using system. xml. serialization;
Namespace theater
{
[Xmlroot ("theater")]
Public class theater
{
[Xmlelement ("name")]
Public string name = "";
[Xmlelement ("phone")]
Public String phone = "";
[Xmlelement ("movie")]
Public movie [] movies;
Public override string tostring ()
{
String movies = "";
If (movies! = NULL)
Foreach (movie in movies)
Movies + = "\ n" + movie. tostring ();
Return string. Format ("{0} \ n {1} \ n {2}", name, phone, movies );
}
}
Xmlroot embedding maps a class of theater to the root element theater of XML. xmlelement embedding maps the name, phone, and movies information to the name, phone, and movie XML elements nested in theater. because movies is the movie column, xmlserializer maps it to multiple XML movie elements.
A movie type with implicit ing
public class movie
{< br> [xmlelement ("title")]
Public String title = "";
[xmlattribute ("Minutes")]
Public uint minutes = 0;
[xmlelement ("showing", datatype = "time")]
Public datetime [] showings;
Public override string tostring ()
{< br> string showings = "";
If (showings! = NULL)
{< br> showings = "shows at";
foreach (datetime showing in showings)
showings + = showing. tow.timestring () + "";
}< br> else
{< br> showings = "-No showings";
}< br> return string. format ("{0} ({1} min) {2}", title, minutes, showings);
}< BR >}< br>
Xmlelement ing the title and showings resources to the title and showing XML elements in the movie element. like theater. movie is the movie in the datetime values column. showings is mapped to multiple XML showing elements. the attention of showing data includes the location adequacy data datatype = "time ". it maps the datetime value to an XML time value, and removes the date information while retaining only the time information. xmlattribute annotation maps the Minutes data objects to XML semantics instead of XML elements.
The moviestars and rating elements in the XML data are not mapped to any things in the movie class. when deserializing XML data, xmlserializer simply jumps over the scheme that cannot be mapped. when listing an object in a string, you can add the xmlignore adequacy in the public information and the response you want xmlserializer to skip.
Xmlroot, xmlelement, and xmlattribute should both include the final variables "attribute." In My declarative statements ?, I have used the form of signature with no backend signature. in theater and movie, the public attention can be changed to public attention for better sealing. xmlserializer can use them in the same way. where am I? The use of these products for information is to make the Agency more dynamic.
Convert XML data into objects and import XML data into a theater object? It has become very easy now. the program in table D, xmlin, creates a theater object through the movie showings XML data row through the reverse string. this program runs through the command line. You need to specify an XML file.
Using system;
Using system. xml. serialization;
Using system. IO;
Namespace theater
{
Public class xmlin
{
Public static void main (string [] ARGs)
{
If (ARGs. length! = 1)
{
Console. writeline ("Usage: xmlin infile. xml ");
Return;
}
Try
{
// Deserialize the specified file to a theater object.
Xmlserializer xs = new xmlserializer (typeof (theater ));
Filestream FS = new filestream (ARGs [0], filemode. Open );
Theater theater = (theater) XS. deserialize (FS );
// Display the theater object.
Console. writeline (theater );
}
Catch (exception X)
{
Console. writeline ("exception:" + X. Message );
}
}
}
}
The main program libraries are all placed in the try generation segment of the main function ?. First, create an xmlserializer object and specify a system. type indicates the type of the object to be created by the deserialization program. the typeof operator returns a system for theater. type object. then, open a file stream to fetch the XML file that is imported into the pipeline. use the deserialize method of xmlserializer and upload the file stream to it. deserialize returns a reference to theater object. the tostring method in theater and movie objects allows you to export them in a simple manner.
2. List object strings in XML?
It is easy to generate XML data from a theater object. In table E, xmlout is to list a theater object string to an XML file ?. This program uses the command line to parse the XML file.
Using system;
Using system. xml;
Using system. xml. serialization;
Using system. IO;
Namespace theater
{
Public class xmlout
{
// Returns a populated theater object.
Public static theater gettheater ()
{
Movie movie = new movie ();
Movie. Title = "O Brother, Where art thou? ";
Movie. Minutes = 102;
Movie. showings = new datetime [3];
Movie. showings [0] = new datetime (2001, 8, 2, 13, 15, 0 );
Movie. showings [1] = new datetime (2001, 8, 2, 16, 30, 0 );
Movie. showings [2] = new datetime (2001, 8, 2, 19, 55, 0 );
Theater theater = new theater ();
Theater. Name = "Hollywood movies 10 ";
Theater. Phone = "(972) 555-154 ";
Theater. Movies = new movie [1];
Theater. Movies [0] = movie;
Return theater;
}
Public static void main (string [] ARGs)
{
If (ARGs. length! = 1)
{
Console. writeline ("Usage: xmlout OUTFILE. xml ");
Return;
}
Try
{
Theater theater = gettheater ();
// Serialize the theater object to an XML file.
Xmlserializer xs = new xmlserializer (typeof (theater ));
Filestream FS = new filestream (ARGs [0], filemode. Create );
Xs. serialize (FS, theater );
}
Catch (exception X)
{
Console. writeline ("exception:" + X. Message );
}
}
}
}
Theaterout. xml contents:
Hollywood movies 10
(972) 555-154
13:15:00. 0000000-
16:30:00. 0000000-
19:55:00. 0000000-
The main program libraries are all placed in the try generation segment of the main function ?. First, you can use gettheater to create a theater object. then, open a file stream to generate the generated XML file. the principal uses the serialize method of xmlserializer, and uploads the object stream and theater object to it. this is the form. The XML file is generated!
The exported theater element contains the XML Naming space (xmlns) generated between the template and the template instance naming space. Why are these two naming spaces? These resources do not mean that-in the Showing Element refers to the time in the middle of the United States, or that the GMT time is used for another six hours.
Mobile resources are just a piece of cake
Xmlserializer makes it very easy to move data between objects and XML, As long as XML ing between objects is added to the class. however, for more complex object models, manual creation of XML ing will become very troublesome and easy to export. in my next articleArticle?, I will tell you how to automate this job and implement more granular control over your xml resources.