久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 .NET XmlSerializer 使值類型可以為空?

How to make a value type nullable with .NET XmlSerializer?(如何使用 .NET XmlSerializer 使值類型可以為空?)
本文介紹了如何使用 .NET XmlSerializer 使值類型可以為空?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我有這個對象:

[Serializable]
public class MyClass
{
    public int Age { get; set; }
    public int MyClassB { get; set; }
}
[Serializable]
public class MyClassB
{
    public int RandomNumber { get; set; }
}

XmlSerializer 將像這樣序列化對象:

The XmlSerializer will serialize the object like that:

<MyClass>
    <Age>0</age>
    <MyClassB>
        <RandomNumber>4234</RandomNumber>
    </MyClassB>
</MyClass>

如何使屬性 Age 可以為空?IE:當屬性年齡小于 0 時不序列化它?

How can I made the property Age nullable? IE: to not serialize the property Age when it's under 0?

我嘗試使用 Nullable,但它會像這樣序列化我的對象:

I tried with the Nullable, but it serialize my object like that:

<MyClass>
    <Age d5p1:nil="true" />
    <MyClassB>
        <RandomNumber>4234</RandomNumber>
    </MyClassB>
</MyClass>    

通過閱讀 MSDN 文檔,我發現了這一點:

By reading the MSDN documentation I found this:

您不能將 IsNullable 屬性應用于鍵入為值類型的成員,因為值類型不能包含 nullNothingnullptra 空引用(在 Visual Basic 中為 Nothing).此外,對于可為空的值類型,您不能將此屬性設置為 false.當此類類型為 nullNothingnullptra 空引用(在 Visual Basic 中為 Nothing)時,將通過將 xsi:nil 設置為 true 來序列化它們.

You cannot apply the IsNullable property to a member typed as a value type because a value type cannot contain nullNothingnullptra null reference (Nothing in Visual Basic). Additionally, you cannot set this property to false for nullable value types. When such types are nullNothingnullptra null reference (Nothing in Visual Basic), they will be serialized by setting xsi:nil to true.

來源:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.isnullable.aspx

我知道不能將值類型設置為 null.值類型總是被設置為某物.序列化不能根據它的當前值來決定是否序列化它.

I understand a value type can't be set to null. A valuetype is always set to something. The serialization can't make the decision to serialize it or not based on it's current value.

我嘗試使用這些屬性,但沒有成功.我嘗試創建一個 agecontainer 對象并使用屬性操作它的序列化,但沒有成功.

I tried with the attributes, but it didn't work out. I tried creating an agecontainer object and manipulate it's serialization with attributes, but it didn't work out.

我真正想要的是:

<MyClass>
    <MyClassB>
        <RandomNumber>4234</RandomNumber>
    </MyClassB>
</MyClass>

當屬性年齡低于 0(零)時.

When the property Age is below 0 (zero).

看起來您必須實現自定義序列化.

Looks like you'll have to implement custom serialization.

是的,我也這么認為,但我想沒有它就離開.

Yeah, that's what I though too, but I'd like to get away without it.

在應用中,對象要復雜得多,我不想自己處理序列化.

In the application, the object is much more complex, and I would like to not handle the serialization myself.

推薦答案

我剛剛發現了這個.XmlSerialier 查找 XXXSpecified 布爾屬性以確定是否應包含它.這應該可以很好地解決問題.

I just discovered this. XmlSerialier looks for a XXXSpecified boolean property to determine if it should be included. This should solve the problem nicely.

[Serializable]
public class MyClass
{
  public int Age { get; set; }
  [XmlIgnore]
  public bool AgeSpecified { get { return Age >= 0; } }
  public int MyClassB { get; set; }
}

[Serializable]
public class MyClassB
{
  public int RandomNumber { get; set; }
}

證明:

static string Serialize<T>(T obj)
{
  var serializer = new XmlSerializer(typeof(T));
  var builder = new StringBuilder();
  using (var writer = new StringWriter(builder))
  {
    serializer.Serialize(writer, obj);
    return builder.ToString();
  }
}

static void Main(string[] args)
{
  var withoutAge = new MyClass() { Age = -1 };
  var withAge = new MyClass() { Age = 20 };

  Serialize(withoutAge); // = <MyClass><MyClassB>0</MyClassB></MyClass>
  Serialize(withAge); // = <MyClass><Age>20</Age><MyClassB>0</MyClassB></MyClass>
}


編輯:是的,這是一個記錄在案的功能.請參閱 MSDN 條目 XmlSerializer


Edit: Yes, it is a documented feature. See the MSDN entry for XmlSerializer

另一種選擇是使用特殊模式創建 XmlSerializer 識別的布爾字段,并將 XmlIgnoreAttribute 應用于該字段.該模式以propertyNameSpecified 的形式創建.例如,如果有一個名為MyFirstName"的字段,您還將創建一個名為MyFirstNameSpecified"的字段,以指示 XmlSerializer 是否生成名為MyFirstName"的 XML 元素.

Another option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of propertyNameSpecified. For example, if there is a field named "MyFirstName" you would also create a field named "MyFirstNameSpecified" that instructs the XmlSerializer whether to generate the XML element named "MyFirstName".

這篇關于如何使用 .NET XmlSerializer 使值類型可以為空?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to check if String is null(如何檢查字符串是否為空)
Equals(item, null) or item == null(Equals(item, null) 或 item == null)
Overriding == operator. How to compare to null?(覆蓋 == 運算符.如何與空值進行比較?)
What does the question mark in member access mean in C#?(成員訪問中的問號在 C# 中是什么意思?)
The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 運算符)
C# null coalescing operator equivalent for c++(C# 空合并運算符等效于 C++)
主站蜘蛛池模板: 中文字幕乱码亚洲精品一区 | 日韩视频精品在线 | 亚洲视频中文字幕 | 亚洲男人天堂 | 国产精品视频免费观看 | 欧美亚洲另类丝袜综合网动图 | av影音在线| 91国产视频在线观看 | 亚洲一区二区国产 | 在线一区二区三区 | 亚洲欧美在线观看 | 91精品国产91久久久久久 | 日韩成年人视频在线 | 人人九九精 | 久久这里只有精品首页 | 中文字幕在线免费视频 | 国产精品久久久久久久久大全 | 亚洲导航深夜福利涩涩屋 | 中文字幕一区二区三区精彩视频 | 成人免费在线 | 欧美日韩高清在线一区 | 欧美日韩在线免费观看 | 91文字幕巨乱亚洲香蕉 | 欧美爱爱视频网站 | 天堂免费| 国产福利视频在线观看 | 亚洲精品久久久蜜桃网站 | 毛片视频免费观看 | 精品在线一区二区三区 | 色桃网 | 国产精彩视频一区 | 中文字幕 在线观看 | 色播av| 国产高清在线精品一区二区三区 | 国产区一区 | 黄色中文字幕 | 久久精品中文字幕 | 91一区二区三区在线观看 | www.日韩免费 | 亚洲精品一区二区三区在线 | 色视频网站免费 |