Formatting of XML created by DataContractSerializer

前端 未结 5 1485
不思量自难忘°
不思量自难忘° 2021-02-01 00:35

Is there an easy way to get DataContractSerializer to spit out formatted XML rather then one long string? I don\'t want to change the tags or content in any way, just have it ad

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 01:29

        public static string SerializeEntity(T source)
        {
            using (MemoryStream ms = new MemoryStream())
            {
    
                    NetDataContractSerializer serializer = new NetDataContractSerializer();
                    serializer.Serialize(ms, source);
                    return System.Text.Encoding.ASCII.GetString(ms.ToArray());
    
            }
        }
    
        public static T DeSerializeEntity(string xml)
        {
            using (MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(xml)))
            {
                    NetDataContractSerializer serializer = new NetDataContractSerializer();
                    return (T)serializer.Deserialize(ms);
            }
        }
    

提交回复
热议问题