gpt4 book ai didi

c# - 反序列化空值

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:08 25 4
gpt4 key购买 nike

我正在尝试将一个 xml 文件序列化为一个元素可以为空的类

XML文件

  <Status StatusId="1">
<StatusName>Not inspected</StatusName>
<Acute></Acute>
</Status>
<Status StatusId="2">
<StatusName>Acute</StatusName>
<Acute>1</Acute>
</Status>

 private int _acute;
public int Acute
{
get { return _acute; }
set
{
_acute = value;
NotifyPropertyChanged("Acute");

}
}

我试过int?和 [XmlElement( IsNullable = true )] 但没有人知道如何读取空元素而不会出现任何错误。

最佳答案

我猜这不是正确的方法,但我过去通过处理从 string 的转换来处理这个问题int 我自己。大致:

 private int _acute = 0; // or assign to some more appropriate default
[XmlIgnore]
public int Acute
{
get { return _acute; }
set
{
_acute = value;
NotifyPropertyChanged("Acute");
}
}
// I kept mine as public but may be able to hide it at a minimal level that
// serialization still works.
[XmlElement(ElementName = "Acute")]
public string SerializedAcute
{
get { return _acute.ToString(); }
set
{
Int32.TryParse(value, out _acute)
}
}

如果你有很多这样的东西,虽然它可能会变得有点丑陋。

关于c# - 反序列化空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9523929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com