使用Web服务时出现以下错误:无法序列化System.Nullable`1 [System.Decimal]类型的成员'XXX'.XmlAttribute/XmlText不能用于编码复杂类型.
我理解错误并在此博客上找到了解决方案:http: //www.jamesewelch.com/2009/02/03/how-to-serialize-subsonic-objects-with-nullable-properties/#more-827
我想使用解决方案2,正如您在博客上的评论中所看到的,我没有太多运气.我正在使用ExcuteTypeList来恢复数据.
任何指针或帮助都会很棒.
谢谢
我实现IXmlSerializable了下面的类型,它将RGB颜色值编码为单个字符串:
public class SerializableColor : IXmlSerializable
{
public int R { get; set; }
public int G { get; set; }
public int B { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
var data = reader.ReadString();
reader.ReadEndElement();
var split = data.Split(' ');
R = int.Parse(split[0]);
G = int.Parse(split[1]);
B = int.Parse(split[2]);
}
public void WriteXml(XmlWriter writer)
{
writer.WriteString(R + " " + G + " " + B);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个用于Xml序列化的类.
在其中我有一个可以使用XmlAttribute修饰的可空属性:
[XmlAttribute("lastUpdated")]
public DateTime? LastUpdated { get; set; }
Run Code Online (Sandbox Code Playgroud)
如果属性为null或为空,如何忽略序列化的属性?
我已经尝试过以下但是当有值时它不会序列化(总是忽略):
[XmlIgnore]
public DateTime? LastUpdatedValue { get; set; }
[XmlAttribute("lastUpdated")]
public DateTime LastUpdated { get; set; }
public bool ShouldSerializeLastUpdated()
{
return LastUpdatedValue.HasValue;
}
Run Code Online (Sandbox Code Playgroud)