• C# xml序列化 datatime字段


         [XmlIgnore]
            public DateTime ApplicationDatetime { get; set; }
    
            [XmlElement("ApplicationDatetime")]
            public string ApplicationDatetimeString
            {
                get { return ApplicationDatetime.ToString("yyyy-MM-dd HH:mm:ss"); }
                set { ApplicationDatetime = DateTime.Parse(value); }
            }
            static public string SerializeToXml<T>(T obj)
            {
                string xmlString = string.Empty;
                //XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                //using (MemoryStream ms = new MemoryStream())
                //{
                //    xmlSerializer.Serialize(ms, obj);
                //    xmlString = Encoding.UTF8.GetString(ms.ToArray());
                //}
                Encoding encoding = Encoding.UTF8;
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
                    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                    namespaces.Add("", "");
    
                    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, encoding);
    
                    xmlTextWriter.Formatting = Formatting.None;
                    xmlSerializer.Serialize(xmlTextWriter, obj, namespaces);
                    xmlTextWriter.Flush();
                    xmlTextWriter.Close();
    
                    xmlString = encoding.GetString(memoryStream.ToArray());
                }
                return xmlString;
            }
                string queryParameters = WebUtils.SerializeToXml(posQueryParameters);
    <Entity><EntityName>我和coffee</EntityName><PId>110101198505012511</PId><LoanId>c70215bbc6dd4722b51d2bb93c1a81bd</LoanId><BankCardNo /><MobilePhone /><CheckEmployee>dkx4003</CheckEmployee><ApplicationDatetime>2019-11-07 15:01:52</ApplicationDatetime><Role>PR</Role></Entity>
  • 相关阅读:
    LabVIEW入门第三天(软件及驱动包下载)
    poj1732
    poj1722
    poj1821
    poj1770
    poj1949
    poj1726
    poj1699
    poj1682
    poj1853
  • 原文地址:https://www.cnblogs.com/hofmann/p/11812610.html
Copyright © 2020-2023  润新知