实体序列化生成个性XML ,生成XML节点/属性

前端之家收集整理的这篇文章主要介绍了实体序列化生成个性XML ,生成XML节点/属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

创建实体

[XmlRoot("root")]
    public class XML_root
    {
        public string template { get; set; }
        public XML_content content = new XML_content();

        [System.Xml.Serialization.XmlIgnore]
        public List<XML_AchDetail> listdetail = new List<XML_AchDetail>();
    }

    [XmlType("content")]  
    public class XML_content
    {
        public NAttribute SPCX = new NAttribute();
        public NAttribute JBFY = new NAttribute();
    }

    public class NAttribute
    {
        [System.Xml.Serialization.XmlAttribute]
        public string tablename = "S_XS";

        [System.Xml.Serialization.XmlAttribute]
        public string type = "varchar";

        [System.Xml.Serialization.XmlText]
        public string value { get; set; }
    }

     public class XML_AchDetail
    {
        [System.Xml.Serialization.XmlText]
        public string value { get; set; }
    }

XML实体转换类

public class XmlSerializeHelper
    {
        public static string Serialize<T>(T obj)
        {
            return Serialize<T>(obj,Encoding.UTF8);
        }

        /// <summary> 
        /// 实体对象序列化成xml字符串 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="obj"></param> 
        /// <returns></returns> 
        public static string Serialize<T>(T obj,Encoding encoding)
        {
            try
            {
                if (obj == null)
                    throw new ArgumentNullException("obj");

                var ser = new XmlSerializer(obj.GetType());
                using (var ms = new MemoryStream())
                {
                    using (var writer = new XmlTextWriter(ms,encoding))
                    {
                        writer.Formatting = Formatting.Indented;
                        ser.Serialize(writer,obj);
                    }
                    var xml = encoding.GetString(ms.ToArray());
                    xml = xml.Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"","");
                    xml = xml.Replace(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"","");
                    return xml;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary> 
        /// 反序列化xml字符为对象,默认为Utf-8编码 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="xml"></param> 
        /// <returns></returns> 
        public static T DeSerialize<T>(string xml)
            where T : new()
        {
            return DeSerialize<T>(xml,Encoding.UTF8);
        }

        /// <summary> 
        /// 反序列化xml字符为对象 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="xml"></param> 
        /// <param name="encoding"></param> 
        /// <returns></returns> 
        public static T DeSerialize<T>(string xml,Encoding encoding)
            where T : new()
        {
            try
            {
                var mySerializer = new XmlSerializer(typeof(T));
                using (var ms = new MemoryStream(encoding.GetBytes(xml)))
                {
                    using (var sr = new StreamReader(ms,encoding))
                    {
                        return (T)mySerializer.Deserialize(sr);
                    }
                }
            }
            catch (Exception e)
            {
                return default(T);
            }
        }
    }

调用函数

XML_root bean = new XML_root();
            bean.template = "这里填写内容";
 string strxml = XmlSerializeHelper.Serialize<XML_root>(bean);

生成的xml格式为

<?xml version="1.0" encoding="GB2312"?>
<root>
  <content>
    <SPCX tablename="S_XS" type="varchar">0001号</SPCX >
    <JBFY tablename="S_XS" type="varchar" />
  </content>
  <template>这里填写内容</template>
</root>
原文链接:https://www.f2er.com/xml/293669.html

猜你在找的XML相关文章