C#对象XML序列化

1.Xml序列化操作类

.Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象。

以下代码仅供参考:

    public class XmlSerializerTest
    {
        public static void SaveToXml(string file, object data)
        {
            XmlSerializer serializer = new XmlSerializer(data.GetType());
            using (XmlTextWriter fs = new XmlTextWriter(file, Encoding.UTF8))   // 如果输出中文,记得字符集
            {
                fs.Formatting = System.Xml.Formatting.Indented;     // 输出后保留换行符等。默认为None,即过滤换行符等 
                serializer.Serialize(fs, data);
            }
        }
        public static object LoadFromXml(string file, Type type)
        {
            object value = null;
            XmlSerializer serializer = new XmlSerializer(type);
            using (XmlTextReader fs = new XmlTextReader(file))
            {
                value = serializer.Deserialize(fs);
            }
            return value;
        }
    }

2.对象与Xml的关系

2.1对象的默认设置

输出对象时,仅输出Public标识的属性,如下代码:(此处仅说明默认情况,下面会有介绍xml标记)

    public class TestObject
    {
        public string ID { set; get; }
        public string Name { set; get; }
        public string Desc { set; get; }
        public List<string> Tags { set; get; }
        public List<ChildObject> Children { set; get; }
    }

    public class ChildObject
    {
        public string ID { set; get; }
        public string Name { set; get; }
        public string Desc { set; get; }
    }

输出后,文件内容是:

<?xml version="1.0" encoding="utf-8"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>1</ID>
  <Name>测试对象</Name>
  <Desc>此为描述
此处换行显示</Desc>
  <Tags>
    <string>标签A</string>
    <string>标签B</string>
    <string>标签C</string>
  </Tags>
  <Children>
    <ChildObject>
      <ID>1-1</ID>
      <Name>子A</Name>
      <Desc>此处
换行</Desc>
    </ChildObject>
    <ChildObject>
      <ID>1-2</ID>
      <Name>子B</Name>
      <Desc>此处不换行</Desc>
    </ChildObject>
  </Children>
</TestObject>

2.2对象的序列化标记

[XmlRootAttribute("TestObject", Namespace = "Sy4w.Test", IsNullable = false)]
public class TestObject                       // 序列化此类时,定义为Xml根节点信息

[XmlAttribute("ID")]
public string ID { set; get; }               //  作为TestObject标签的属性。如:<ClassID="..." />

[XmlElementAttribute("Desc", IsNullable = false)]
public string Desc { set; get; }            //  作为类标签的内容。如<Class>...</Class>

[XmlElementAttribute("Tags", IsNullable = false)]
public List<string> Tags { set; get; }  //   作为类标签的子节点。如<Class><Tags>...</Tags></Class>

[XmlArrayAttribute("Children")]
public List<ChildObject> Children { set; get; }  // 作为类标签的多级子标签。如<Class><Children><ChildObject>...</ChildObject></Children></Class>

[XmlIgnoreAttribute]
public string Name { set; get; }           //   此属性忽略

用代码感受下吧

    [XmlRootAttribute("TestObject", Namespace = "Sy4w.Test", IsNullable = false)]
    public class TestObject
    {
        [XmlAttribute("ID")]
        public string ID { set; get; }

        [XmlIgnoreAttribute]
        public string Name { set; get; }
        [XmlElementAttribute("Desc", IsNullable = false)]
        public string Desc { set; get; }
        [XmlElementAttribute("Tags", IsNullable = false)]
        public List<string> Tags { set; get; }

        [XmlArrayAttribute("Children")]
        public List<ChildObject> Children { set; get; }
    }

    public class ChildObject
    {
        [XmlAttribute("ID")]
        public string ID { set; get; }
        [XmlIgnoreAttribute]
        public string Name { set; get; }
        [XmlElementAttribute("Desc", IsNullable = true)]
        public string Desc { set; get; }
    }

生成的Xml文件示例:

<?xml version="1.0" encoding="utf-8"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="1" xmlns="Sy4w.Test">
  <Desc>此为描述
此处换行显示</Desc>
  <Tags>标签A</Tags>
  <Tags>标签B</Tags>
  <Tags>标签C</Tags>
  <Children>
    <ChildObject ID="1-1">
      <Desc>此处
换行</Desc>
    </ChildObject>
    <ChildObject ID="1-2">
      <Desc>此处不换行</Desc>
    </ChildObject>
  </Children>
</TestObject>

 

posted on 2016-10-06 19:25  Sy4w  阅读(332)  评论(0编辑  收藏  举报

导航