• C#对象序列化成XML,以及自定义标签名


    C#对象序列化操作:

    public class XMLHelper
    {
        /// <summary>
        /// 对象序列化成 XML String
        /// </summary>
        public static string XmlSerialize<T>(T obj)
        {
            string xmlString = string.Empty;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            //using (TextWriter textWriter = new StreamWriter("D:\xmlTest.xml"))
            //{
            //    xmlSerializer.Serialize(textWriter, obj);
            //}
            using (MemoryStream ms = new MemoryStream())
            {
                xmlSerializer.Serialize(ms, obj);
                xmlString = Encoding.UTF8.GetString(ms.ToArray());
            }
            return xmlString;
        }
    
        /// <summary>
        /// XML String 反序列化成对象
        /// </summary>
        public static T XmlDeserialize<T>(string xmlString)
        {
            T t = default(T);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
            {
                using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                {
                    Object obj = xmlSerializer.Deserialize(xmlReader);
                    t = (T)obj;
                }
            }
            return t;
        }
    
    }

    示例:

    [Serializable]
    [XmlType(TypeName = "MySong")]
    public class Song
    {
        [XmlElement("A")]
        public string Artist;
        [XmlElement("S")]
        public string SongTitle;
    }
    
    class XMLDemo
    {
        static void Main(string[] args)
        {
            List<Song> list = new List<Song>()
            {
                new Song(){ SongTitle="歌曲标题1",Artist="歌手1"},
                new Song(){ SongTitle="歌曲标题2",Artist="歌手2"},
                new Song(){ SongTitle="歌曲标题3",Artist="歌手3"}
            };
            string xmlString = XMLHelper.XmlSerialize(list);
            /*<?xml version="1.0"?>
            <ArrayOfSong xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <Song>
                <Artist>歌手1</Artist>
                <SongTitle>歌曲标题1</SongTitle>
              </Song>
              <Song>
                <Artist>歌手2</Artist>
                <SongTitle>歌曲标题2</SongTitle>
              </Song>
              <Song>
                <Artist>歌手3</Artist>
                <SongTitle>歌曲标题3</SongTitle>
              </Song>
            </ArrayOfSong>
             
            // 自定义标签名
            <?xml version="1.0"?>
            <ArrayOfMySong xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <MySong>
                <A>歌手1</A>
                <S>歌曲标题1</S>
              </MySong>
              <MySong>
                <A>歌手2</A>
                <S>歌曲标题2</S>
              </MySong>
              <MySong>
                <A>歌手3</A>
                <S>歌曲标题3</S>
              </MySong>
            </ArrayOfMySong>
            */
            Console.WriteLine(xmlString);
        }
    }
  • 相关阅读:
    图论算法之查找强分支
    图论算法之拓扑排序的简单代码
    图论算法之最短路径
    由函数符号找不到联想到的动态链接过程
    linux下cp覆盖原so文件时引起的段错误原因确定
    java 中能否使用 动态加载的类(Class.forName) 来做类型转换?
    为什么java不支持多重继承?
    static inner class 什么时候被加载
    thinking in java 读书笔记 --- overriding private method
    java List排序方法 Comparator和Comparable
  • 原文地址:https://www.cnblogs.com/lusunqing/p/3968114.html
Copyright © 2020-2023  润新知