我有一个可以为空的int类?数据类型设置为序列化为xml元素.有没有办法设置它,所以如果值为null,xml序列化程序将不会序列化该元素?
我试图添加[System.Xml.Serialization.XmlElement(IsNullable = false)]属性,但我得到一个运行时序列化异常,说有一个反映类型的错误,因为"IsNullable可能不会设置为'false '对于Nullable类型.考虑使用'System.Int32'类型或从XmlElement属性中删除IsNullable属性."
[Serializable]
[System.Xml.Serialization.XmlRoot("Score", Namespace = "http://mycomp.com/test/score/v1")]
public class Score
{
private int? iID_m;
...
/// <summary>
///
/// </summary>
public int? ID
{
get
{
return iID_m;
}
set
{
iID_m = value;
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
上面的类将序列化为:
<Score xmlns="http://mycomp.com/test/score/v1">
<ID xsi:nil="true" />
</Score>
Run Code Online (Sandbox Code Playgroud)
但对于null的ID,我根本不需要ID元素,主要是因为当我在MSSQL中使用OPENXML时,对于看起来像的元素,它返回0而不是null
我有一个类,我想在编辑器中更改其属性。所以我创建了我的类 System.Serializable 并公开了我希望能够更改的变量。
像这样:
[System.Serializable]
public class UIOptionsRing
{
public float Radius, DistanceBetweenPoints, StartOffset, GapInDegrees;
public int? GapAfterElementNumer = 3; //this var doesnt show up
public Vector3 CircleCenter;
public GameObject CircleElementsContainer;
}
Run Code Online (Sandbox Code Playgroud)
但我遇到的问题是 GapAfterElementNumer 没有显示在编辑器中,而所有其他字段都是。我怎样才能让它int?也显示出来?