C#实体生成xml格式

发布时间:2024年01月05日

public string XmlSerialize<T>(T obj)
??????? {
??????????? System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));

??????????? MemoryStream stream = new MemoryStream();
??????????? System.Xml.XmlWriterSettings setting = new System.Xml.XmlWriterSettings();
??????????? setting.Encoding = new UTF8Encoding(false);
??????????? setting.Indent = true;//开启缩进
??????????? using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stream, setting))
??????????? {
??????????????? XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces(
??????????????? new XmlQualifiedName[] {
??????????????? new XmlQualifiedName(string.Empty, string.Empty)
??????????????? });
??????????????? //writer.WriteStartDocument(true);
??????????????? xs.Serialize(writer, obj, _namespaces);
??????????? }

??????????? return Encoding.UTF8.GetString(stream.ToArray());
??????? }

调用:

文章来源:https://blog.csdn.net/weixin_46256404/article/details/133827117
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。