Use generics and common methods to convert XML to Object

Source: Internet
Author: User
Tags static class

This period of time has not been idle, the overall can be divided into the following 2 parts of the study.

1:ormcodehelper's Reference and learning.

2:MVC3 of learning.

For Ormcodehelper (Keny), full use of plug-in development, Plug and Play, personal feeling or relatively cool, the architecture is good. It includes the generation of SQL Server 2000,sql Server 2005 and related C # code for Oracle. Brother than a year ago wrote that many cows, haha, the elder brother last year disorderly wrote a webpage version of (http://www.cnblogs.com/jasenkin/archive/2010/02/11/1667511.html), now look a year ago code, brother Sigh, Who wrote the code, the TMD is really bad writing!!! Of course, Ormcodehelper compared with Codesmith, there is still a gap between the OH. Brother Lin used to give me the Codesmith template (n-tier), the elder brother has no time to look carefully, brother know that template can be all the code generated, including n-tier code, stored procedures, pages and so on. Although the time is like the cleavage, as long as squeeze a crowded always still have! But, brother really ... Originally 9th Brother is to take leave to rest, alas, again brother work ....

There is the Mvc3,razor is really too much to force, throw away MVC2 bar, haha, @ indeed very good.

In Ormcodehelper, the usage of the configuration file is good, the elder brother to apply, refine a generic type out (in fact, the most important is the framework of plug-in development). For the conversion of XML to object, here is a serialize method. In fact, there is another way to convert XML into objects through reflection, but the approach to corporate * * * is still not written. Of course, that way brother can be written horizontally (because the elder brother has been back in the heart), Haha, general code ....

Look at the code first, as follows:

public static class Serializer {public static void serialize<t> (String filePath, t[] array) whe Re t:new () {if (string).
                IsNullOrEmpty (FilePath) | | Array = = null| | Array.
            length==0) {return;
                try {xmlserializerfactory xmlserializerfactory = new Xmlserializerfactory (); XmlSerializer XmlSerializer = Xmlserializerfactory.createserializer (array. GetType (), typeof (T).
                Name);
                Stream stream = new FileStream (FilePath, FileMode.Create);
                Xmlserializer.serialize (stream, array); Stream.
            Close (); The catch {}} public static void Serialize (string FilePath, Object obj ) {if (string. IsNullOrEmpty (FilePath) | |
            obj = = null) {return;
            } try{Xmlserializerfactory xmlserializerfactory = new Xmlserializerfactory (); XmlSerializer XmlSerializer = Xmlserializerfactory.createserializer (obj. GetType (), obj. GetType ().
                Name);
                Stream stream = new FileStream (FilePath, FileMode.Create);
                Xmlserializer.serialize (stream, obj); Stream.
            Close (); } Catch {}}}
public static list<t> deserialize<t> (String filePath) where T:new () {list<t> ResU
            Lts=new list<t> (); if (string. IsNullOrEmpty (FilePath) | |!
            File.exists (FilePath)) {return results;
            Object obj = null;
                try {xmlserializerfactory xmlserializerfactory = new Xmlserializerfactory (); XmlSerializer XmlSerializer = Xmlserializerfactory.createserializer (typeof (T[)), typeof (T).
                Name);
                Stream stream = new FileStream (FilePath, System.IO.FileMode.Open);
                obj = Xmlserializer.deserialize (stream); Stream.
                Close (); Results.
            AddRange (obj as t[]);
        Catch {} return results; public static object Deserialize (string FilePath, Type targettype) {if (strIng. IsNullOrEmpty (FilePath) | |! File.exists (FilePath) | |
            TargetType = = null) {return null;
            Object obj = null;
                try {xmlserializerfactory xmlserializerfactory = new Xmlserializerfactory ();
                XmlSerializer XmlSerializer = Xmlserializerfactory.createserializer (TargetType, targettype.name);
                Stream stream = new FileStream (FilePath, FileMode.Open);
                obj = Xmlserializer.deserialize (stream); Stream.
            Close ();
        Catch {} return obj; }

From the above 4 methods, you can see that the main thing is to serialize objects into XML by XmlSerializer and deserialize XML into objects, which is simpler and easier to use.

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.