xml序列化与反序列化_Xml序列化通用列表<>

xml序列化与反序列化

xml序列化与反序列化

There's lots of good techniques out there for using the XmlSerializer to serialize a Generic List<> type. You can implement IXmlSerializable and some other trickiness.

使用XmlSerializer序列化Generic List <>类型有很多好的技术。 您可以实现IXmlSerializable和其他一些技巧。

Here's the simplest possible thing I could do to make it work while working on a hobby project over lunch today with a friend.

这是我今天与朋友共进午餐时从事一项业余爱好项目时可以做的最简单的事情。

UPDATE: I'm a dork and didn't see the forest for the trees on this one. I don't need a parallel array at all. That's my old 1.1 brain creeping in. Thanks to folks in the comments. It works fine just like this. I'm not sure why I thought it didn't work when I tried it before. Thanks folks!

更新:我是一个傻瓜,没有看到森林上的树木。 我根本不需要并行数组。 那是我的旧1.1大脑在蠕动。感谢其他人的评论。 像这样工作正常。 我不确定为什么我以前尝试过时认为它不起作用。 谢谢大家!

namespace Poo
{
    public class Foo
    {
        public Foo() { FooReadings = new List(); }

        [XmlArray("FooReadings")] //Even this attribute isn't really needed if you accept the default.
public List<FooReading> FooReadings; } public class FooReading { public FooReading() { } public FooReading(DateTime date, decimal thing2) { this.Thing2 = thing2; this.Date = date; } public decimal Thing2; public DateTime Date; } }

The technique below is useful for other things, but not in this instance.

下面的技术可用于其他用途,但在这种情况下无效。


    
    
     
     namespace Poo
{
    public class Foo
    {
        public Foo() { FooReadings = new List(); }

        [XmlIgnore]
        public List<FooReading> FooReadings;

        [XmlArray("FooReadings")]
        public FooReading[] ArrayFooReadings
        {
            get { return FooReadings.ToArray(); }
            set { FooReadings = new List(value); }
        }
    }

    public class FooReading
    {
        public FooReading() { }

        public FooReading(DateTime date, decimal thing2)
        {
            this.Thing2 = thing2;
            this.Date = date;
        }

        public decimal Thing2;
        public DateTime Date;
    }
}

    
    

The "Real List<>" is ignored and the "fake" one is presented as an Array in the Getters/Setter. Not pretty, to be sure.

“ Real List <>”将被忽略,而“伪造”列表将在Getters / Setter中显示为数组。 当然不漂亮。

What better ways are there that you prefer?

您更喜欢哪种更好的方法?

翻译自: https://www.hanselman.com/blog/xmlserializing-a-generic-listltgt

xml序列化与反序列化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我把这段xml,用@Data注解转成java bean实体,每个字段上都要加上@XStreamAlias注解 <FLIGHT> <FID>b59bb8fa48ad6edbf35f7f468019d06d</FID> <DATA_TYPE>操作类型:INSEERT、UPDATE、DELETE</DATA_TYPE> <FLIGHT_ID>0或者例如:ZH9847-2020-08-18-A</FLIGHT_ID> <AIRLINE_IATA>ZH</AIRLINES_IATA> <AIRLINE_ICAO>CSZ</AIRLINES_ICAO> <FLIGHT_NO>ZH9487</FLIGHT_NO> <DAP_AP>CTU</DAP_AP> <AAP_AP>JJN</AAP_AP> <FLINES>JJN,HGH,SJW</FLINES> <REG_NUMBER>B3207</REG_NUMBER> <FLIGHT_STATUS>0</FLIGHT_STATUS> <FLIGHT_DATE>20200820</FLIGHT_DATE> <PLAN_TAKEOFF_TIME>20200820153628<PLAN_TAKEOFF_TIME> <PLAN_LANDIN_TIME>20200820153628</PLAN_LANDIN_TIME> <ESTIMATED_TAKEOFF_TIME>20200820153628</ESTIMATED_TAKEOFF_TIME> <ESTIMATE_LANDIN_TIME>20200820153628</ESTIMATE_LANDIN_TIME> <REAL_TAKEOFF_TIME>20200820153628</REAL_TAKEOFF_TIME> <REAL_LANDIN_TIME>20200820153628</REAL_TAKEOFF_TIME> <FLY_DISTANCE>1302</FLY_DISTANCE> <DEP_STAND>22</DEP_STAND> <ARR_STAND>32</ARR_STAND> <TOUCH_DOWN_RUNWAY>03</TOUCH_DOWN_RUNWAY> <TAKE_OFF_RUNWAY>03</TAKE_OFF_RUNWAY> <DEP_GATE>09</DEP_GATE> <CHECKIN_COUNTER></CHECKIN_COUNTER> <CHECKIN_COUNTER2></CHECKIN_COUNTER2> <LUGGAGE_TURNTABLE>2DA</LUGGAGE_TURNTABLE> <LUGGAGE_TURNTABLE2>2DB</LUGGAGE_TURNTABLE2> <DORI>0</DORI> <MISSION_PROPERTY>W/Z</MISSION_PROPERTY> <IS_SHARE>0</IS_SHARE> <SHARE_MAIN_FLIGHT>ZH8701</SHARE_MAIN_FLIGHT> <IS_VIP>0</IS_VIP> <ALNAP></ALNAP> <FIRST_POINT></FIRST_POINT> <AIRWAY_POINTS> </AIRWAY_POINTS> <ROUTE></ROUTE> <DELAY_REASON_PUID>0</DELAY_REASON_PUID> <DELAY_REASON_CLIENT></DELAY_REASON_CLIENT> <DELAY_TYPE></DELAY_TYPE> <DELAY_REASON></DELAY_REASON> </FLIGHT>
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值