c# xml序列化和還原序列化。也就是xml的解析和反解析。

來源:互聯網
上載者:User

標籤:manage   enc   大致   blog   序列   public   linq   manager   頭部   

用習慣了newTownSoft.json 的json還原序列化。碰到xml是真的不習慣。

每次json還原序列化都是直接把json丟到bejson網站產生一個實體類,稍微修改修改一點點變數名。然後直接newTownSoft還原序列化,一下就得到一個實體類了。今天調某個介面,碰到xml。

記錄如下。

xml

<response>
<functionID>setItemsPics</functionID>
<time>2017-09-07 15:51:04</time>
<ItemsIDList>
  <itemIDInfo>
    <operCode>0</operCode>
    <operation>操作成功</operation>
  </itemIDInfo>
<itemIDInfo>
  <itemID>1125507057</itemID>
    <operCode>0</operCode>
    <operation>操作成功</operation>
  </itemIDInfo>
</ItemsIDList>
</response>

實體類如下:先引用 using System.Xml.Serialization;

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Serialization;namespace DDTopManager.Response{    [XmlRoot("response")]    public class RSPItemPicSet    {        //操作返回xml樣本,按這個xml格式定義可序列化的實體類        //        public string str = @"<response>        //    <functionID>setItemsPics</functionID>        //    <time>2017-09-07 15:51:04</time>        //    <ItemsIDList>        //        <itemIDInfo>        //            <itemID>1125550957</itemID>        //            <operCode>0</operCode>        //            <operation>操作成功</operation>        //        </itemIDInfo>        //        <itemIDInfo>        //            <itemID>1125507057</itemID>        //            <operCode>0</operCode>        //            <operation>操作成功</operation>        //        </itemIDInfo>        //    </ItemsIDList>        //</response>";        [XmlElement("functionID")]        public string functionID { get; set; }        [XmlElement("time")]        public string time { get; set; }        [XmlArray("ItemsIDList"), XmlArrayItem("itemIDInfo")]        public List<itemIDInfo> ItemsIDList { get; set; }        [XmlRoot("itemIDInfo")]        public class itemIDInfo        {            [XmlElement("itemID")]            public string itemID { get; set; }            [XmlElement("operCode")]            public string operCode { get; set; }            [XmlElement("operation")]            public string operation { get; set; }        }    }}

  引用(這是從str這個xml中還原序列化到實體類)大致如下:

XmlSerializer se = new XmlSerializer(typeof(RSPOrdersListGet));byte[] arry = Encoding.UTF8.GetBytes(str);System.IO.Stream me = new System.IO.MemoryStream(arry);RSPOrdersListGet rsp = (RSPOrdersListGet)se.Deserialize(me);

  序列化如下:

 RSPOrdersListGet RSPLG = new RSPOrdersListGet();            RSPLG.functionID = "setItemsPics";            List<itemIDInfo> itemidinfos = new List<itemIDInfo>();            itemIDInfo idi = new itemIDInfo();            idi.operation = "000";            idi.operCode = "0011";            idi.itemId = "aaa";            itemidinfos.Add(idi);            itemIDInfo idi1 = new itemIDInfo();            idi1.operation = "000";            idi1.operCode = "0011";            idi1.itemId = "bbb";            itemidinfos.Add(idi1);            RSPLG.ItemsIDList = itemidinfos;            XmlSerializer rSPOrdersListGet = new XmlSerializer(typeof(RSPOrdersListGet));            rSPOrdersListGet.Serialize(Console.Out, RSPLG);

  

記錄,以免忘記。

有一點和newTownSoft.json序列化和還原序列化不同的是,比如數字List<Entity> MyData 這個MyData 和json要對的上,

這裡的直接用 頭部的約束來定義即可

c# xml序列化和還原序列化。也就是xml的解析和反解析。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.