簡體   English   中英

如何在xml序列化中顯示數據類型?

[英]How to show data types in xml serialization?

我有這個課程:

public class Computer 
    {
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }

        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }

        public string IPAddress { get; set; }
        public string Name { get; set; }
    }

並且我需要xml以顯示某些元素中的數據類型(dt:dt =“ string”):

<fpc4:Computer StorageName="{D37291CA-D1A7-4F34-87E4-8D84F1397BEA}" StorageType="1">
        <fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer1</fpc4:Name>
    </fpc4:Computer>

有什么建議么?

您可以在充當子元素的類中創建一個屬性,以充當數據類型屬性,並使用對Give對象的反射來執行以下操作。 MethodBase具有一個稱為ReturnType的屬性,該屬性將為您提供返回類型。

[XmlAttribute("DataType")]
public string DataType 
{
    get 
    { 
        return typeof(IPAddress).GetProperty("DataType").GetGetMethod().ReturnType.ToString();
    }
}

這將產生以下xml行

<fpc4:Computer StorageName="{D37291CA-D1A7-4F34-87E4-8D84F1397BEA}" StorageType="1">
        <fpc4:IPAddress DataType="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer1</fpc4:Name>
</fpc4:Computer>

注意IPAddress元素上的DataType =“ string”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM