【C#】使用C#将类序列化为XML_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 【C#】使用C#将类序列化为XML

【C#】使用C#将类序列化为XML

 2015/4/3 10:28:59  rizo  程序员俱乐部  我要评论(0)
  • 摘要:直接上代码:publicstaticclassXmlSerializer{publicstaticvoidSaveToXml(stringfilePath,objectsourceObj,Typetype){if(!string.IsNullOrWhiteSpace(filePath)&&sourceObj!=null){type=type!=null?type:sourceObj.GetType();using
  • 标签:C# 使用 XML 序列化

直接上代码:

class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"> 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>
发表评论
用户名: 匿名