我正在将一个 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 吗?还是有其他方法?