本文介绍了从SQL数据库获取数据并将其加载到arraylist中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数据集从数据库加载数据,该数据集将数据保存到arraylist/array

The data is loaded from the database using a dataset which saves the data to the arraylist/array

What code can I use to achieve this?

推荐答案

using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace  Put.Here.Your.Proyects.Namespace
{
    public class Serializer
    {
        public static string Serialize(Type typeOfObj, object obj)
        {
            Stream memStm = new MemoryStream();
            StreamWriter tw = new StreamWriter(memStm);

            XmlSerializer serializadorXML = new XmlSerializer(typeOfObj);
            serializadorXML.Serialize(tw, obj);
            memStm.Position = 0;

            StreamReader stmRdr = new StreamReader(memStm);

            string text = stmRdr.ReadToEnd();
            tw.Close();
            return text.Trim();
        }

        public static object Deserialize(Type typeOfObj, string text)
        {
            object obj = null;

            ////Optional code
            //XmlReaderSettings settings = new XmlReaderSettings();
            //settings.ConformanceLevel = ConformanceLevel.Fragment;
            //settings.IgnoreWhitespace = true;
            //settings.IgnoreComments = true;
            //settings.IgnoreProcessingInstructions = true;

            byte[] byteArray = Encoding.UTF8.GetBytes(text);
            MemoryStream stream = new MemoryStream(byteArray);

            XmlReader xmlReader = XmlReader.Create(stream);//, settings);
            XmlSerializer serializadorXML = new XmlSerializer(typeOfObj);

            if (serializadorXML.CanDeserialize(xmlReader))
            {
                obj = serializadorXML.Deserialize(xmlReader);
            }

            return obj;
        }
    }
}



使用typeof进行序列化,例如:



to serialize use typeof like example:

string xmlText = Serialize(typeOf miObjectClassName, myObject);



取消演员表的异化:



to deserealize cast object:

miObjectClassName myObject = (miObjectClassName)Deserialize(typeOf miObjectClassName, xmlText);



您可以将static标签移除为功能,但我不建议您这样做,因为您可以像实用程序一样使用功能,而无需创建对象:



you can remove static label to fuction but I recomend not because you can use fuction like utility without need to create object:

string x = Put.Here.Your.Proyects.Namespace.Serialize(typeOf miObjectClassName, myObject);


这篇关于从SQL数据库获取数据并将其加载到arraylist中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:42