一个下面的类:
     [Serializable]
    public class Col
    {
        private string name;
        private string value;        public Col()
        {
        }        public Col(string name, string value)
        {
            this.name = name;
            this.value = value;
        }        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
    }假如有如下对象:    Col col = new Col("a", "v");使用XmlSerializer可以序列化成下面两种样式:
一:
<Col name="a" value="v"/>二:
<Col>
<name>a</name>
<value>v</value>
</Col>请问有什么方法序列化成下面样式:<Col name="a">v</Col>

解决方案 »

  1.   

    Xml序列化可以设置属性.   [XmlAttribute()]
        public string Name
          {
                get { return this.name; }
                set { this.name = value; }
           } 第二种,普通的Xml序列化就好了 
    标记[Serializable] 属性是深序列化,不能添加上面的属性,
    如果只是序列化公共属性和字段还是使用Xml序列化好了 
      

  2.   

    按我下面的设置就可以了 [Serializable]
    public class Col
    {
    private string name;
    private string value; public Col()
    {
    } public Col( string name, string value )
    {
    this.name = name;
    this.value = value;
    }
    [XmlAttribute]
    public string Name
    {
    get { return this.name; }
    set { this.name = value; }
    }
    [XmlText]
    public string Value
    {
    get { return this.value; }
    set { this.value = value; }
    }