澳门新葡萄京娱乐场WebService序列化DataTable

        public static List GetWorkExperience(string userId)
        {
            List workExperience = new List();
            Unity unity = Unity.GetInstance();
            DataTable table = new DataTable();
            unity.GetTable(out table);

    [WebMethod]
    public DataTable ShowUSer( string connectionString )
    {
        DataTable dt = new DataTable( “User” );
        XmlSerializer ser = new XmlSerializer( typeof( DataTable ) );

            dr = tables.NewRow();
            dr[“UserId”] = “0001”;
            dr[“CompanyName”] = “WireSoft”;
            dr[“Seniority”] = “2012.02-2012.05”;
            tables.Rows.Add(dr);
            dr = tables.NewRow();
            dr[“UserId”] = “0001”;
            dr[“CompanyName”] = “Jin Xun”;
            dr[“Seniority”] = “2009.07-2011.02”;
            tables.Rows.Add(dr);
            dr = tables.NewRow();
            dr[“UserId”] = “0002”;
            dr[“CompanyName”] = “Hua Wei”;
            dr[“Seniority”] = “2011.07-“;
            tables.Rows.Add(dr);
            dt = tables.Copy();
        }
        public  void CreateTable()
        {
            dc = new DataColumn(“UserId”,
System.Type.GetType(“System.String”));
            tables.Columns.Add(dc);
            dc = new DataColumn(“companyName”,
System.Type.GetType(“System.String”));
            tables.Columns.Add(dc);
            dc = new DataColumn(“seniority”,
System.Type.GetType(“System.String”));
            tables.Columns.Add(dc);
        }
    }

            DataTable schema = reader.GetSchemaTable( );

            var experiences = (from experience in table.AsEnumerable()
                               where experience.Field(“UserId”) ==
userId
                               select new
                               {
                                   companyName =
experience.Field(“CompanyName”),
                                   seniority =
experience.Field(“Seniority”)
澳门新葡萄京娱乐场,                               }).ToList();
            experiences.ForEach(e => workExperience.Add(new
WorkExperience() { companyName = e.companyName, seniority = e.seniority
}));
            return workExperience;
        }
    }
    public class Unity
    {
        public static DataTable tables = new DataTable();
        public static DataRow dr;
        public static DataColumn dc = new DataColumn();
        public static object objLock = new object();
        public static Unity unityInstance;
        private Unity()
        {

        }

概念:XML序列化是将公共字段和属性转化为序列格式(这里指XML),以便存储或传输的过程。反序列化则是从XML中重新创建原始状态的对象.
复制代码 代码如下:
    class SerializeDemo
    {
        static void Main()
        {
            EmployeeCollection employeeCollection = new
EmployeeCollection()
            {
                Employees = Employeer.Employees()
            };
            XmlSerializer serialize = new
XmlSerializer(typeof(EmployeeCollection));
            string filePath = @”E:PProjectTestEmployee.xml”;
             SerializeEmployee(serialize, filePath,
employeeCollection);
            DeserializeEmployee(serialize, filePath);
        }
        static void SerializeEmployee(XmlSerializer serialize, string
filePath, EmployeeCollection employeeCollection)
        {
            using (FileStream fs = new FileStream(filePath,
FileMode.Create, FileAccess.Write))
            {
                serialize.Serialize(fs, employeeCollection);
            }
        }
        static void DeserializeEmployee(XmlSerializer serialize,string
filePath)
        {
            using (FileStream fs = new FileStream(filePath,
FileMode.Open, FileAccess.Read))
            {
                EmployeeCollection collection =
(EmployeeCollection)serialize.Deserialize(fs);
                collection.Employees.ForEach(e =>
Console.WriteLine(“Name:{0},Gender:{1},Age:{2},Education:{3}”,
e.userName, e.gender, e.age, e.education));
            }
        }
    }
    [Serializable]
    public class EmployeeCollection
    {
        public List Employees { get; set; }
    }
    [Serializable]
    public class Employeer
    {
        public string userId { get; set; }
        public string userName { get; set; }
        public string gender { get; set; }
        public int age { get; set; }
        public List workExperience { get; set; }
        public string education { get; set; }
        public static List Employees()
        {
           return new List()
           {
                new Employeer()
                {  
                    userId = “0001”,
                    userName = “guoHu”,
                    gender=”Man”,
                    age=25,education=”underGraduate”,
                    workExperience =
WorkExperience.GetWorkExperience(“0001”)
                }
           };

    [WebMethod]
    public DataTable Show( string filename)
    {
        XmlSerializer ser = new XmlSerializer( typeof( DataTable ) );
        DataTable table = new DataTable( “User” );
        DataColumn col = new DataColumn( “string” );

        }
    }
    [Serializable]
    public class WorkExperience
    {
        public string userId { get; set; }
        public string companyName { get; set; }
        public string seniority { get; set; }

            reader.Close( );
            dt.AcceptChanges( );

        }
        public static Unity GetInstance()
        {
            if (unityInstance == null)
            {
                lock (objLock)
                {
                    if (unityInstance == null)
                    {
                        unityInstance = new Unity();
                    }
                }
            }
            return unityInstance;
        }
        public void GetTable(out DataTable dt)
        {
            unityInstance.CreateTable();

        table.Columns.Add( col );
        DataRow r;

        foreach ( DataRow r in dt.Rows )
        {
          
            for ( int i = 0; i < dt.Columns.Count; i++ )
            {
                Console.Write( “{0}t”, r[ i ] );
            }
            Console.WriteLine( );
        }

        for ( int i = 0; i < 10; i++ )
        {
            r = table.NewRow( );
            r[ 0 ] = “元” + i;
            table.Rows.Add( r );
        }

            foreach ( DataRow r in schema.Rows )
            {
                DataColumn col = new DataColumn(
                    r[ “columnName” ] as string,
                    r[ “DataType” ] as Type
                    );
                dt.Columns.Add( col );
                //dt.Rows.Add( col );
            }

        TextWriter writer = new StreamWriter( connectionString );
        ser.Serialize( writer, dt );
        return dt;
    }

        connectionString = “Data
Source=1c8a2f252c6e43e;database=cnblogs;uid=sa;pwd=sa”;
        string sqlstr = @”SELECT  [userId]
      ,[userName]
      ,[password]
      ,[displayName]
      ,[Email]
      ,[gender]
  FROM [cnblogs].[dbo].[User]”;

        using ( SqlConnection conn = new SqlConnection( connectionString
) )
        {
          
            conn.Open( );
            Console.WriteLine( “连接成功” );
            SqlCommand comm = new SqlCommand( sqlstr, conn );
            SqlDataReader reader = comm.ExecuteReader( );

            while ( reader.Read( ) )
            {
                DataRow row = dt.NewRow( );
                for ( int i = 0; i < dt.Columns.Count; i++ )
                {
                    row[ i ] = reader[ i ];
                    Console.Write( “”, dt.Columns.ToString( ) );
                }
                dt.Rows.Add( row );
            }

        TextWriter writer = new StreamWriter( filename );
        ser.Serialize( writer, table );
        return table;
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注