コード例 #1
0
        public virtual T GetShipmentByText(string text)
        {
            var shipmentText = Regex.Match(text, @"<Shipment>[\s\S]*?</Shipment>").Value.ToString();
            var headerText   = Regex.Match(text, @"<SenderID>(\w*?)</SenderID>[\s]+?<RecipientID>(\w*?)</RecipientID>").Groups;
            var SenderID     = headerText[1].Value;
            var RecipientID  = headerText[2].Value;
            var shipment     = new T();
            var Xshipment    = XElement.Parse(shipmentText);

            ExtractXMLDynamic.SetInstanceByXElement(shipment, Xshipment);
            return(shipment);
        }
コード例 #2
0
        /// <summary>
        /// 根据T, 在element中查找,T类型的节点,最后返回 T的列表,比如要返回   List<MileStone> 就传入 <MeilStoneColletion>...</MeilStoneColletion>元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="element"></param>
        /// <returns></returns>
        public static List <T> SearchCollectionNew <T>(XElement element) where T : class, new()
        {
            var tList = new List <T>();

            if (element != null && element.HasElements)
            {
                dynamic t     = null;
                var     tName = typeof(T).Name;
                var     nName = element.Name.ToString();

                if (nName.Contains(tName))
                {
                    //var eName = nName.Replace("Collection", "");
                    var children = element.Elements(tName);   //获取所有子节点
                    var props    = typeof(T).GetProperties(); //T的属性列表
                    foreach (var child in children)
                    {
                        t = CreateInstance(typeof(T));
                        foreach (var prop in props)
                        {
                            if (prop.PropertyType.IsClass && prop.PropertyType.Name != "String")
                            {
                                Type    classType = prop.PropertyType;         //获得属性当前的类类型
                                dynamic obj       = CreateInstance(classType); //用反射根据传入类型生成对象
                                //var classProps = classType.GetProperties();//获取此类型对象的属性列表
                                XElement cElement = null;
                                if (!prop.PropertyType.Name.Contains("Collection")) //属性类型不是集合时进入
                                {
                                    cElement = child.Element(prop.Name);            //此类对应的元素
                                    var classProps = classType.GetProperties();     //获取此类型对象的属性列表

                                    if (cElement != null && cElement.HasElements)
                                    {
                                        ExtractXMLDynamic.SetNotCollectionElement(obj, cElement, classProps); //把obj 对象传入进行值设置
                                    }
                                    prop.SetValue(t, obj);                                                    //把属性元素对象设置到目标对象t
                                }
                                else
                                {
                                    var subCollectionName       = prop.PropertyType.Name;                                //子Collection名称
                                    var subColletctionChildName = subCollectionName.Replace("Collection", string.Empty); //去掉Collectin后的子元素名称
                                    var subCollection           = child.Element(subCollectionName);                      //获取子Collection元素

                                    //此处可以扩展,如果有其它的情况可以加入其它的条件判断
                                    if (subColletctionChildName == typeof(RegistrationNumber).Name)
                                    {
                                        var subInstance = new RegistrationNumber();
                                        var tSubList    = SearchSubCollection(subInstance, subCollection);
                                        SetInstanceSubCollection(t, tSubList);
                                    }
                                }
                            }

                            else
                            {
                                prop.SetValue(t, child.Element(prop.Name).Value);
                            }
                        }

                        tList.Add(t);
                        if (nName == "RegistrationNumberCollection")
                        {
                            var test = tList;
                        }
                        if (nName == "OrganizationAddressCollection")
                        {
                            var test = tList;
                        }
                    }
                }
            }

            return(tList);
        }