开发者

Object list serialization problem

开发者 https://www.devze.com 2023-03-20 04:39 出处:网络
i have a very specific problem with the mapping of an xml file to an object. Here is the XML example file:

i have a very specific problem with the mapping of an xml file to an object.

Here is the XML example file:

<spyce>
   <reference name="test">
      <Data id="1">10</Data>
      <Data id="2">20</Data>
   </reference>
</Spyce>

Here are my mapping classes:

[Serializable]
[XmlRoot("spyce")]
public class Spyce
{
    private Reference reference;
    [XmlElement("reference")]
    public Reference Reference
    {
        get { return reference; }
        set { reference = value; }
    }
}

[XmlRoot("reference")]
public class Reference
{
    [XmlAttribute("name")]
    public string name;

    [XmlArray]
    private List<Data> dataList;
    [XmlElement("data")]
    public List<Data> DataList
    {
        get { return dataList; }
        set { dataList = value; }
    }
}

public class Data
{
    [XmlAttribute("id")]
    public string id;

    private int dataValue;
    [XmlElement("data")]
    public int DataValue
    {
        get { return dataValue; }
        set { dataValue= value; }
    }
}

The serialization goes well i get an Spyce object. In this object is the Reference object with the variable name and the list DataList. An here comes the problem => the Data objects in the list have a variable id but DataValue stands always at 0.

How can开发者_如何转开发 i handle this?

With best regards.

Spike


Just change attribute to XmlText:

[XmlText(Type=typeof(int))]
public int DataValue
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号