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

來源:互聯網
上載者:User
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還原序列化的程式碼範例詳情介紹的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.