XML中有部分是这样的
<Image Type="6"> <Locations> <Location WaterMark="0" Size="1">http://www.elongstatic.com/gp1/M00/CD/0F/o4YBAFIpMPaAEtObAACGqn9c6fI484.jpg?v=20120311012520</Location> <Location WaterMark="0" Size="2">http://www.elongstatic.com/gp1/M00/46/D7/pIYBAFIlrCyAU5WBAAAO-eg59bg460.jpg?v=20120308231459</Location> <Location WaterMark="0" Size="3">http://www.elongstatic.com/gp1/M00/34/45/pYYBAFIlrCyAKsyGAAAgsG_sj0g560.jpg?v=20120308231459</Location> <Location WaterMark="1" Size="7">http://www.elongstatic.com/gp1/M00/2E/F7/pYYBAFIlrC2AFjvIAAFy5gcQib0455.png?v=20120308231459</Location> </Locations> </Image>
然后我在反序列化的时候创建的对应类结构是这样的
public class Image { [XmlAttribute] public int Type { get; set; } [XmlArrayAttribute] public Location[] Locations { get; set; } } [XmlRootAttribute("Locations")] public class Location { [XmlElementAttribute("Location", IsNullable = false)] public string LocationInfo { get; set; } [XmlAttribute] public string WaterMark { get; set; } [XmlAttribute] public string Size { get; set; } }
然后XML中的Location节点内部的值是个图片地址,我为什么在反序列化的时候获取不到这个值?
请问是否是我的类结构描述错误还是特性加错了?其他节点的属性和子节点都能正常获取,就是类似这种<xxx>……</xxx>中的值获取不到!
感谢解答
<Location WaterMark="1" Size="7">http://www.elongstatic.com/gp1/M00/2E/F7/pYYBAFIlrC2AFjvIAAFy5gcQib0455.png?v=20120308231459</Location>
WaterMark="1" Size="7"是特性不是属性,这个Location是一个属性,这个属性是一个字符串类型,这个属性有2个特性标识
WaterMark和size
那么 我的model应该怎么修改才能获取到值?
@然后、没所以: 没做过`,我猜的,
public class Image { [XmlAttribute] public int Type { get; set; } [XmlArrayAttribute] public Location[] Locations { get; set; } }
你这个Type上加XmlAttribute应该是对的,但Locations就不要加了因为他是本来就存在的属性.
Location类也同理
@吴瑞祥:
public class Image { [XmlAttribute] public int Type { get; set; } public List<Location> Locations { get; set; } } public class Location { [XmlElementAttribute("Location", IsNullable = false)] public string LocationInfo { get; set; } [XmlAttribute] public string WaterMark { get; set; } [XmlAttribute] public string Size { get; set; } }
改成这样还是获取不到 Location 里面的值啊!郁闷
@然后、没所以:
public class Location { [XmlElementAttribute("Location", IsNullable = false)] public string LocationInfo { get; set; } [XmlAttribute] public string WaterMark { get; set; } [XmlAttribute] public string Size { get; set; } }
这个[XmlElementAttribute("Location", IsNullable = false)]不要加Attribute了.Attribute就是特性,你要取的元素的值而不是元素特性的值
@吴瑞祥: 不行,及时去掉还是无法获取到元素的值,是不是实体类数据结构有问题
@然后、没所以: 不是把[XmlElementAttribute("Location", IsNullable = false)]去掉.是改成
[XmlElement]
@吴瑞祥:
我已经尝试改为[XmlElement]了 但是还是取不到值 [XmlElement("Location")]我也试过了
@然后、没所以: 我错了``是[XmlText].我还去百度了下
http://www.cnblogs.com/hobinly/archive/2012/04/01/2132816.html
@吴瑞祥: 哎呀 太感谢你,终于获取到了 谢天谢地你来了~