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

如何使用 .NET XmlSerializer 使值類(lèi)型可以為空?

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

問(wèn)題描述

假設(shè)我有這個(gè)對(duì)象:

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

XmlSerializer 將像這樣序列化對(duì)象:

The XmlSerializer will serialize the object like that:

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

如何使屬性 Age 可以為空?IE:當(dāng)屬性年齡小于 0 時(shí)不序列化它?

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

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

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

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

通過(guò)閱讀 MSDN 文檔,我發(fā)現(xiàn)了這一點(diǎn):

By reading the MSDN documentation I found this:

您不能將 IsNullable 屬性應(yīng)用于鍵入為值類(lèi)型的成員,因?yàn)橹殿?lèi)型不能包含 nullNothingnullptra 空引用(在 Visual Basic 中為 Nothing).此外,對(duì)于可為空的值類(lèi)型,您不能將此屬性設(shè)置為 false.當(dāng)此類(lèi)類(lèi)型為 nullNothingnullptra 空引用(在 Visual Basic 中為 Nothing)時(shí),將通過(guò)將 xsi:nil 設(shè)置為 true 來(lái)序列化它們.

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.

來(lái)源:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.isnullable.aspx

我知道不能將值類(lèi)型設(shè)置為 null.值類(lèi)型總是被設(shè)置為某物.序列化不能根據(jù)它的當(dāng)前值來(lái)決定是否序列化它.

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.

我嘗試使用這些屬性,但沒(méi)有成功.我嘗試創(chuàng)建一個(gè) agecontainer 對(duì)象并使用屬性操作它的序列化,但沒(méi)有成功.

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>

當(dāng)屬性年齡低于 0(零)時(shí).

When the property Age is below 0 (zero).

看起來(lái)您必須實(shí)現(xiàn)自定義序列化.

Looks like you'll have to implement custom serialization.

是的,我也這么認(rèn)為,但我想沒(méi)有它就離開(kāi).

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

在應(yīng)用中,對(duì)象要復(fù)雜得多,我不想自己處理序列化.

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

推薦答案

我剛剛發(fā)現(xiàn)了這個(gè).XmlSerialier 查找 XXXSpecified 布爾屬性以確定是否應(yīng)包含它.這應(yīng)該可以很好地解決問(wèn)題.

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>
}


編輯:是的,這是一個(gè)記錄在案的功能.請(qǐng)參閱 MSDN 條目 XmlSerializer


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

另一種選擇是使用特殊模式創(chuàng)建 XmlSerializer 識(shí)別的布爾字段,并將 XmlIgnoreAttribute 應(yīng)用于該字段.該模式以propertyNameSpecified 的形式創(chuàng)建.例如,如果有一個(gè)名為MyFirstName"的字段,您還將創(chuàng)建一個(gè)名為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".

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

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

相關(guān)文檔推薦

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?(覆蓋 == 運(yùn)算符.如何與空值進(jìn)行比較?)
What does the question mark in member access mean in C#?(成員訪問(wèn)中的問(wèn)號(hào)在 C# 中是什么意思?)
The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 運(yùn)算符)
C# null coalescing operator equivalent for c++(C# 空合并運(yùn)算符等效于 C++)
主站蜘蛛池模板: 国产综合视频 | 欧美日韩国产中文 | 国产视频一区二区三区四区 | 久久五月婷 | 狠狠操狠狠爱 | 国产精品久久久国产盗摄 | 九九影视理伦片 | a级黄毛片 | 国产美女av | 91欧美| 日本国产欧美 | 欧美成人综合 | 久久精品伊人 | 美国式禁忌14在线 | 黄色精品网站 | 中国免费毛片 | 波多野结衣一区二区三区在线观看 | av基地网 | 国产黄色在线观看 | 99婷婷| 福利网站在线观看 | 伊人网在线 | 玉足女爽爽91 | 国产日韩一区二区 | 欧美成人精品欧美一级私黄 | 欧洲精品一区二区 | 成人激情视频 | 国产精品综合网 | 双性呜呜宫交受不住了h | 在线免费观看黄 | 久久福利社 | 免费福利在线观看 | 成人免费网站 | 韩日中文字幕 | 日韩特级片 | av在线精品| 成年免费视频黄网站在线观看 | 死神来了4无删减版在线观看 | 日本香蕉视频 | 欧美视频一区二区三区 | 国产一级黄色大片 |