首页 新闻 会员 周边

这个xml反序列化的模型类怎么写...

0
悬赏园豆:10 [已解决问题] 解决于 2012-09-06 16:35
<Result success="true" message="good">
   <Item ID="3" UserName="u1"/>
   <Item ID="2" UserName="u2"/>
</Result>

 

不清楚序列化的模型要怎么创建...

public class Result
{
    [XmlAttribute("success")]
    public string Success{ get; set; }
    [XmlAttribute("message")]
    public string Message { get; set; }
    
         .....
         Item怎么搞?
         .....
}
xml
wdwwtzy的主页 wdwwtzy | 初学一级 | 园豆:114
提问于:2012-09-06 16:13
< >
分享
最佳答案
0

很好理解,Item其实就是一个类,里面两个属性。例如

public class Result
{
    [XmlAttribute("success")]
    public bool Success{ get; set; }
    [XmlAttribute("message")]
    public string Message { get; set; }
    
    public Item[] Item{get;set;}
}
public class Item
{
    [XmlAttribute("ID")]
    public int ID{get;set;}
    [XmlAttribute("UserName ")]
    public string UserName {get;set;}
}

具体样式自己微调吧。

收获园豆:5
秦楼东 | 小虾三级 |园豆:913 | 2012-09-06 16:25

good 谢楼上两位,我之前用List<>不行,原来用数组啊

wdwwtzy | 园豆:114 (初学一级) | 2012-09-06 16:35

@wdwwtzy: 用List<>的话,你可以这样试下:

[XmlArray]

public List<Item> Items{get;set;}

 

[XmlArray]

public Item[] Items{get;set;}

Launcher | 园豆:45045 (高人七级) | 2012-09-07 17:06

呃,sorry各位,有点错误

如果直接使用public Item[] Item { get; set; }的话,序列化的结果会是

<Item>

  <Item Id="... />

</Item>

这样,经过实验,应该给Item数组加上[XmlElement]特性

wdwwtzy | 园豆:114 (初学一级) | 2012-09-25 17:33
其他回答(1)
0

public Item[] Item{get;set;}

收获园豆:5
Launcher | 园豆:45045 (高人七级) | 2012-09-06 16:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册