[C #] use C # to serialize a class into XML,

Source: Internet
Author: User

[C #] use C # to serialize a class into XML,

Directly run the Code:

 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;        }
 
Model class, the data structure to be serialized:
// [XmlRoot ("sequence")] public class Sequence {// [XmlAttribute ("id")] public string Id {get; set ;} // [XmlElement ("media")] public Media {get; set;} public Sequence () {Id = "Sequence 2"; Media = new Media ();}} // [XmlRoot ("media")] public class Media {// [XmlAttribute ("id")] public string Id {get; set ;} // [XmlElement ("video")] public Video {get; set ;}// [XmlElement ("audio")] public 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 ";}}

 

Actual use:

class Program   {       static void Main(string[] args)       {           Sequence model = new Sequence();           XmlSerializer.SaveToXml("text.xml", model, model.GetType());       }   }

 

Result:

<? Xml version = "1.0" encoding = "UTF-8"?> <Sequence> <Id> Sequence 2 </Id> <Media> <Id> media one </Id> <Video> <name> video 12 </name> </Video> <Audio> <name> video 12 </name> </Audio> </Media> </Sequence>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.