1

我正在将一个 C# 类序列化为 XML。类如下图所示:

public class EventProperty
{
    private string propertyNameField;
    private string propertyValueField;

    public string PropertyName
    {
        get  { return this.propertyNameField; }
        set  { this.propertyNameField = value; }
    }

    [System.Xml.Serialization.XmlTextAttribute()]        
    public string PropertyValue
    {
        get { return this.propertyValueField;  }
        set { this.propertyValueField = value; }
    }
}//end class 

测试代码是:

propertySet = new List<EventProperty>();
propertySet.Add(new EventProperty() { PropertyName = "ChenYi", PropertyValue = "21" });
propertySet.Add(new EventProperty() { PropertyName = "ChenJida", PropertyValue = "232" });
propertySet.Add(new EventProperty() { PropertyName = "XieChao", PropertyValue = "999" });

结果是这样的:

<property>
    <PropertyName>ChenYi</PropertyName>21</property>
<property>
    <PropertyName>ChenJida</PropertyName>232</property>
<property>
    <PropertyName>XieChao</PropertyName>999</property>

但我期待如下所示:

<property>
    <ChenYi>21</ChenYi></property>
<property>
    <ChenJida>232</ChenJida></property>
<property>
    <XieChao>999</XieChao></property>

有人知道应该在这里使用哪个 xmlattribute 吗?还是有其他方法?

4

1 回答 1

0

如果默认的 Xml 序列化行为不能满足您的方案,您应该使用自定义序列化行为。

于 2012-10-18T07:58:06.143 回答