程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 【C#】使用C#將類序列化為XML,

【C#】使用C#將類序列化為XML,

編輯:C#入門知識

【C#】使用C#將類序列化為XML,


直接上代碼:

 public static class XmlSerializer
    {
        public static void SaveToXml(string filePath, object sourceObj, Type type)
        {
            if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
            {
                type = type != null ? type : sourceObj.GetType();

                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                    XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
                    
                    nameSpace.Add("", ""); //not ot output the default namespace
                    xmlSerializer.Serialize(writer, sourceObj, nameSpace);
                }
            }
        }

        public static object LoadFromXml(string filePath, Type type)
        {
            object result = null;

            if (File.Exists(filePath))
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                    result = xmlSerializer.Deserialize(reader);
                }
            }
            return result;
        }
 
模型類,要序列化的數據結構:
 //[XmlRoot("sequence")]
    public class Sequence
    {
        //[XmlAttribute("id")]
        public string Id { get; set; }

        // [XmlElement("media")]
        public Media Media { get; set; }

        public Sequence()
        {
            Id = "序列2";
            Media = new Media();
        }

    }

 //[XmlRoot("media")]
    public class Media
    {
       // [XmlAttribute("id")]
        public string Id { get; set; }

        //[XmlElement("video")]
        public Video Video { get; set; }

       // [XmlElement("audio")]
        public Audio Audio { get; set; }

        public Media()
        {
            Id = "media one";
            Video = new Video();
            Audio = new Audio();
        }
    }

 public class Video
    {
        public string name { get; set; }

        public Video()
        {
            name = "video 12";
        }
    }

  public class Audio
    {
        public string name { get; set; }

        public Audio()
        {
            name = "video 12";
        }
    }

 

實際使用:

class Program
   {
       static void Main(string[] args)
       {
           Sequence model = new Sequence();

           XmlSerializer.SaveToXml("text.xml", model, model.GetType());
       }
   }

 

結果:

<?xml version="1.0" encoding="utf-8"?>
<Sequence>
  <Id>序列2</Id>
  <Media>
    <Id>media one</Id>
    <Video>
      <name>video 12</name>
    </Video>
    <Audio>
      <name>video 12</name>
    </Audio>
  </Media>
</Sequence>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved