본문 바로가기

프로그래밍/C#

xmlParser개발 완료

반응형

Class 를 던지면 해당 Class Property에 대하여 xml을 생성해주는 모듈

 

        public static void CreateXml(object obj, string fileName)
        {
            try
            {
                Type typeofobj = obj.GetType();
                XmlSerializer serializer = new XmlSerializer(typeofobj);
                using (TextWriter writer = new StreamWriter(fileName))
                {
                    serializer.Serialize(writer, obj);
                    writer.Close();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        //public static void CreateXml(List<object> obj, string fileName)
        //{
        //    try
        //    {
        //        //LD.Log.Info("Excute CreateXml");
        //        Type typeofobj = obj.GetType();
        //        XmlSerializer serializer = new XmlSerializer(typeofobj);
        //        using (TextWriter writer = new StreamWriter(fileName))
        //        {
        //            serializer.Serialize(writer, obj);
        //            writer.Close();
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        //LD.Log.Error("CreateXmlModView : " + ex.ToString());
        //        Console.WriteLine(ex);
        //    }
        //}

        public static object ReadXml(object obj, string strfileName)
        {
            try
            {
                Type typeofobj = obj.GetType();
                XmlSerializer Deserializer = new XmlSerializer(typeofobj);
                using (TextReader reader = new StreamReader(strfileName))
                {
                    obj = Deserializer.Deserialize(reader);
                    return obj;
                }
            }
            catch (Exception ex)
            {
                return obj;
            }
        }

        public static object ReadXml(Type typeofobj, string strfileName)
        {
            try
            {
                XmlSerializer Deserializer = new XmlSerializer(typeofobj);
                using (TextReader reader = new StreamReader(strfileName))
                {
                    object obj = Deserializer.Deserialize(reader);
                    return obj;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

 

반응형