首頁  >  文章  >  後端開發  >  C# xml反序列化的程式碼範例詳情介紹

C# xml反序列化的程式碼範例詳情介紹

黄舟
黄舟原創
2017-03-03 11:47:281190瀏覽

XML反序列化很方便,範例:

 [XmlRoot(Root = "result")]
    public class UniMsgSetResult
    {
        [XmlAttribute("resultCode")]
        public int resultCode;


        [XmlElement("uniMsgSet")]
        public UniMsgSet uniMsgSet;

    }
<result resultCode="0">
<UniMsgSet>...</UniMsgSet>
</result>

 集合類型範例:

[XmlRoot("result")]
    public class GetDiskInnerResult
    {
        public string parentCatalogID; //String32 待查询目录的父目录ID。如果当前目录为root,则父目录ID为空。


        


        [XmlArray("catalogList"), XmlArrayItem("catalogInfo")]
        public List<CatalogInfo> catalogList;// CatalogInfo[] 查询节点下的目录列表


        [XmlArray("contentList"), XmlArrayItem("contentInfo")]
        public List<ContentInfo> contentList; // ContentInfo[] 查询节点下的内容列表
    }
 <result>
    <parentCatalogID>1</parentCatalogID>
    <catalogList>
    <catalogInfo>...</catalogInfo>
    <catalogInfo>...</catalogInfo>
    </catalogList>


    <contentList>
    <contentInfo>...</contentInfo>
    <contentInfo>...</contentInfo>
    </contentList>
    </result>

如果要為集合實體新增屬性:

In order words, add an attribute to an object element after xml serialization,
If you want something like,

#
<Rats count=“2″>
  <Rat>little rat</Rat>
  <Rat>old rat</Rat>
</Rats>

The C# code is

#

[XmlType(“Rats”)]
    public class Rats
    {
        [XmlAttribute(“count”)]
        public int Count { get; set; }
        [XmlElement(“Rat”)] // now the array element will be as same as the object element Rats. 
        public string[] Rat { get; set; }
    }


Traditional xml array serialization would get the extra element for the array itself.

[XmlType(“Rats”)]
    public class Rats
    {
        [XmlAttribute(“count”)]
        public int Count { get; set; }
        [XmlArray(“Rats”)]
        [XmlArrayItem(“Rat”)]
        public string[] Rat { get; set; }
    }
<Rats count=“2″>
  <Rats>
    <Rat>little rat</Rat>
    <Rat>old rat</Rat>
  </Rats>
</Rats>

 以上就是C#  xml反序列化的程式碼範例詳情介紹的內容,更詳細介紹的內容,更詳細就是C#  xml反序列化的程式碼範例詳情介紹的內容,更詳細多相關內容請關注PHP中文網(www.php.cn)!


####
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn