Exemplo n.º 1
0
        /// <summary>
        /// 对象序列化成 XML String
        /// </summary>
        public static string XmlSerialize(ExFields exFields)
        {
            if (exFields == null)
            {
                return(" ");
            }
            if (exFields.Count <= 0)
            {
                return(" ");
            }

            string        xmlString     = string.Empty;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ExFields));

            using (MemoryStream ms = new MemoryStream())
            {
                xmlSerializer.Serialize(ms, exFields);
                xmlString = Encoding.UTF8.GetString(ms.ToArray());
            }
            return(xmlString);
        }
Exemplo n.º 2
0
        /// <summary>
        /// XML String 反序列化成对象
        /// </summary>
        public static ExFields XmlDeserialize(string xmlString)
        {
            ExFields fields = new ExFields();

            xmlString = xmlString.Trim();
            if (string.IsNullOrEmpty(xmlString))
            {
                return(fields);
            }

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ExFields));

            using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
            {
                using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                {
                    Object obj = xmlSerializer.Deserialize(xmlReader);
                    fields = (ExFields)obj;
                }
            }
            return(fields);
        }