码迷,mamicode.com
首页 > 其他好文 > 详细

重写xml特性。

时间:2015-03-19 23:46:53      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:


        private readonly static List<XmlSerializerModel> propertylist = new List<XmlSerializerModel>() {
                #region FlightProductGroupList
                new XmlSerializerModel{ XmlArrayAttributeName="FlightProductGroupList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SearchResultTypeDomesticIPS),
                    XmlArrayItemsName="FlightProductGroupType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightProductGroupType)},

                new XmlSerializerModel{ XmlArrayAttributeName="FlightSegmentList", XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightProductGroupType),
                    XmlArrayItemsName="FlightSegmentType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightSegment)},

                new XmlSerializerModel{ XmlArrayAttributeName="FlightProductList", XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightProductGroupType),
                    XmlArrayItemsName="int", XmlArrayItemType=typeof(int)},
                #endregion
                #region FlightListGroupList
                new XmlSerializerModel{ XmlArrayAttributeName="FlightListGroupList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SearchResultTypeDomesticIPS),
                    XmlArrayItemsName="OrigDestFlightListGroupType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.OrigDestFlightListGroupType)},

                new XmlSerializerModel{ XmlArrayAttributeName="FlightList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.OrigDestFlightListGroupType),
                    XmlArrayItemsName="FlightEntityType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightEntityType)},

                new XmlSerializerModel{ XmlArrayAttributeName="FlightFeeInfoList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightEntityType),
                    XmlArrayItemsName="FlightFeeInfoTypeDomestic", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightFeeInfoTypeDomestic)},


                new XmlSerializerModel{ XmlArrayAttributeName="FeeList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightFeeInfoTypeDomestic),
                    XmlArrayItemsName="FeeInfoType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FeeInfoType)},

                new XmlSerializerModel{ XmlArrayAttributeName="SubClassList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightEntityType),
                    XmlArrayItemsName="SubClassEntityListType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SubClassEntityListType)},

                new XmlSerializerModel{ XmlArrayAttributeName="FlightStopList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightEntityType),
                    XmlArrayItemsName="FlightStopEntityType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightStopEntityType)},

                new XmlSerializerModel{ XmlArrayAttributeName="StandardPriceList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.FlightEntityType),
                    XmlArrayItemsName="StandardPriceType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.StandardPriceType)},
                #endregion
                #region ProductList
                new XmlSerializerModel{ XmlArrayAttributeName="ProductList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SearchResultTypeDomesticIPS),
                    XmlArrayItemsName="ProductEntityType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.ProductEntityType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="TotalPriceInfoList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.ProductEntityType),
                    XmlArrayItemsName="PriceInfoType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PriceInfoTypeDomestic)},

                new XmlSerializerModel{ XmlArrayAttributeName="PolicyIndexList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.ProductEntityType),
                    XmlArrayItemsName="int", XmlArrayItemType=typeof(int)},
                #endregion
                #region PolicyRuleList
                new XmlSerializerModel{ XmlArrayAttributeName="PolicyRuleList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SearchResultTypeDomesticIPS),
                    XmlArrayItemsName="PolicyRuleType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PolicyRuleType)},
                #endregion
                #region PolicyInfoList
                     new XmlSerializerModel{ XmlArrayAttributeName="PolicyInfoList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SearchResultTypeDomesticIPS),
                    XmlArrayItemsName="PolicyInfoType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PolicyInfoType)},
                     
                      new XmlSerializerModel{ XmlArrayAttributeName="PostCityList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.DeliveryInfoType),
                    XmlArrayItemsName="string", XmlArrayItemType=typeof(string)},

                    new XmlSerializerModel{ XmlArrayAttributeName="DeliveryCityList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.DeliveryInfoType),
                    XmlArrayItemsName="string", XmlArrayItemType=typeof(string)},

                    new XmlSerializerModel{ XmlArrayAttributeName="ItineraryPriceInfoList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PolicyInfoType),
                    XmlArrayItemsName="PriceInfoType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PriceInfoTypeDomestic)},

                    new XmlSerializerModel{ XmlArrayAttributeName="ApplicableTravelerCategoryList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SaleControlEntityType),
                    XmlArrayItemsName="TravelerCategoryCodeType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.TravelerCategoryCodeType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="AcceptableIDCardTypeList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SaleControlEntityType),
                    XmlArrayItemsName="IDCardType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.IDCardType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="AcceptablePaymentWayList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SaleControlEntityType),
                    XmlArrayItemsName="PaymentWayType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PaymentWayType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="PaymentCardTypeList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PaymentWayType),
                    XmlArrayItemsName="PaymentCardTypeType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PaymentCardTypeType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="AcceptablePaymentCardTypeList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.SaleControlEntityType),
                    XmlArrayItemsName="PaymentCardTypeType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PaymentCardTypeType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="GiftInfoList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.MarketingStrategyEntityType),
                    XmlArrayItemsName="GiftInfoType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.GiftInfoType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="InsuranceInfoList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.MarketingStrategyEntityType),
                    XmlArrayItemsName="InsuranceInfoType", XmlArrayItemType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.InsuranceInfoType)},

                    new XmlSerializerModel{ XmlArrayAttributeName="PolicyRuleIndexList",  XmlArrayAttributeType=typeof(Ctrip.AM.IPS.EntityLib.CtripSDK.PolicyInfoType),
                    XmlArrayItemsName="int", XmlArrayItemType=typeof(int)},
                #endregion
            };

 private class XmlSerializerModel
        {
            public string XmlArrayAttributeName { get; set; }

            public string XmlArrayItemsName { get; set; }

            public Type XmlArrayItemType { get; set; }

            public Type XmlArrayAttributeType { get; set; }

        }

 

 private class XmlSerializerSingleton
        {
            private static XmlSerializer _instance = null;
            private static object _lock = new object();
            private XmlSerializerSingleton()
            {
            }
            public static XmlSerializer GetInstance
            {
                get
                {
                    if (_instance == null)
                    {
                        lock (_lock)
                        {
                            if (_instance == null)
                            {
                                try
                                {
                                    var attrOverrides = new XmlAttributeOverrides();
                                    //遍历静态list
                                    propertylist.ForEach(item =>
                                    {
                                        var attrArray = new System.Xml.Serialization.XmlAttributes();
                                        //定义xmlarray特性
                                        attrArray.XmlArray = new System.Xml.Serialization.XmlArrayAttribute(item.XmlArrayAttributeName);
                                        //xmlarray 添加子节点特性
                                        attrArray.XmlArrayItems.Add(new System.Xml.Serialization.XmlArrayItemAttribute(item.XmlArrayItemsName, item.XmlArrayItemType));
                                        //重写xmlarray特性
                                        attrOverrides.Add(item.XmlArrayAttributeType, item.XmlArrayAttributeName, attrArray);
                                    });
                                    _instance = new XmlSerializer(typeof(SearchResultTypeDomesticIPS), attrOverrides);
                                }
                                catch (Exception)
                                {

                                    throw;
                                }

                            }
                        }
                    }
                    return _instance;
                }
            }
        }
    }

重写xml特性。

标签:

原文地址:http://www.cnblogs.com/chenru1988/p/4352043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!