コード例 #1
0
ファイル: EntityHelper.cs プロジェクト: xslonglianxia/XKD
        // Token: 0x06000139 RID: 313 RVA: 0x00005A5C File Offset: 0x00003C5C
        public static XDocument ConvertEntityToXml <T>(T entity) where T : class, new()
        {
            T arg_17_0;

            if ((arg_17_0 = entity) == null)
            {
                arg_17_0 = Activator.CreateInstance <T>();
            }
            entity = arg_17_0;
            XDocument xDocument = new XDocument();

            xDocument.Add(new XElement("xml"));
            XElement      root    = xDocument.Root;
            List <string> @object = new string[]
            {
                "ToUserName",
                "FromUserName",
                "CreateTime",
                "MsgType",
                "Content",
                "ArticleCount",
                "Articles",
                "FuncFlag",
                "Title ",
                "Description ",
                "PicUrl",
                "Url"
            }.ToList <string>();
            Func <string, int>  orderByPropName = new Func <string, int>(@object.IndexOf);
            List <PropertyInfo> list            = (from p in entity.GetType().GetProperties()
                                                   orderby orderByPropName(p.Name)
                                                   select p).ToList <PropertyInfo>();

            foreach (PropertyInfo current in list)
            {
                string name = current.Name;
                if (name == "Articles")
                {
                    XElement       xElement = new XElement("Articles");
                    List <Article> list2    = current.GetValue(entity, null) as List <Article>;
                    foreach (Article current2 in list2)
                    {
                        IEnumerable <XElement> content = EntityHelper.ConvertEntityToXml <Article>(current2).Root.Elements();
                        xElement.Add(new XElement("item", content));
                    }
                    root.Add(xElement);
                }
                else
                {
                    string name2 = current.PropertyType.Name;
                    if (name2 == null)
                    {
                        goto IL_353;
                    }
                    if (!(name2 == "String"))
                    {
                        if (!(name2 == "DateTime"))
                        {
                            if (!(name2 == "Boolean"))
                            {
                                if (!(name2 == "ResponseMsgType"))
                                {
                                    if (!(name2 == "Article"))
                                    {
                                        goto IL_353;
                                    }
                                    root.Add(new XElement(name, current.GetValue(entity, null).ToString().ToLower()));
                                }
                                else
                                {
                                    root.Add(new XElement(name, current.GetValue(entity, null).ToString().ToLower()));
                                }
                            }
                            else
                            {
                                if (!(name == "FuncFlag"))
                                {
                                    goto IL_353;
                                }
                                root.Add(new XElement(name, ((bool)current.GetValue(entity, null)) ? "1" : "0"));
                            }
                        }
                        else
                        {
                            root.Add(new XElement(name, ((DateTime)current.GetValue(entity, null)).Ticks));
                        }
                    }
                    else
                    {
                        root.Add(new XElement(name, new XCData((current.GetValue(entity, null) as string) ?? "")));
                    }
                    continue;
IL_353:
                    root.Add(new XElement(name, current.GetValue(entity, null)));
                }
            }
            return(xDocument);
        }
コード例 #2
0
        public static XDocument ConvertEntityToXml <T>(T entity) where T : class, new()
        {
            entity = (entity ?? new T());
            XDocument xDocument = new XDocument();

            xDocument.Add(new XElement("xml"));
            XElement      root = xDocument.Root;
            List <string> list = new string[12]
            {
                "ToUserName",
                "FromUserName",
                "CreateTime",
                "MsgType",
                "Content",
                "ArticleCount",
                "Articles",
                "FuncFlag",
                "Title ",
                "Description ",
                "PicUrl",
                "Url"
            }.ToList();
            List <string>       list2           = list;
            Func <string, int>  orderByPropName = list2.IndexOf;
            List <PropertyInfo> list3           = (from p in entity.GetType().GetProperties()
                                                   orderby orderByPropName(p.Name)
                                                   select p).ToList();

            foreach (PropertyInfo item in list3)
            {
                string name = item.Name;
                if (name == "Articles")
                {
                    XElement       xElement = new XElement("Articles");
                    List <Article> list4    = item.GetValue(entity, null) as List <Article>;
                    foreach (Article item2 in list4)
                    {
                        IEnumerable <XElement> content = EntityHelper.ConvertEntityToXml(item2).Root.Elements();
                        xElement.Add(new XElement("item", content));
                    }
                    root.Add(xElement);
                }
                else
                {
                    switch (item.PropertyType.Name)
                    {
                    case "String":
                        root.Add(new XElement(name, new XCData((item.GetValue(entity, null) as string) ?? "")));
                        break;

                    case "DateTime":
                        root.Add(new XElement(name, ((DateTime)item.GetValue(entity, null)).Ticks));
                        break;

                    case "Boolean":
                        if (name == "FuncFlag")
                        {
                            root.Add(new XElement(name, ((bool)item.GetValue(entity, null)) ? "1" : "0"));
                            break;
                        }
                        goto default;

                    case "ResponseMsgType":
                        root.Add(new XElement(name, item.GetValue(entity, null).ToString().ToLower()));
                        break;

                    case "Article":
                        root.Add(new XElement(name, item.GetValue(entity, null).ToString().ToLower()));
                        break;

                    default:
                        root.Add(new XElement(name, item.GetValue(entity, null)));
                        break;
                    }
                }
            }
            return(xDocument);
        }
コード例 #3
0
        public static XDocument ConvertEntityToXml <T>(T entity) where T : class, new()
        {
            T obj = entity;

            if ((object)obj == null)
            {
                obj = Activator.CreateInstance <T>();
            }
            entity = obj;
            XDocument xdocument = new XDocument();

            xdocument.Add((object)new XElement((XName)"xml"));
            XElement           root            = xdocument.Root;
            Func <string, int> orderByPropName = new Func <string, int>(Enumerable.ToList <string>((IEnumerable <string>) new string[13]
            {
                "ToUserName",
                "FromUserName",
                "CreateTime",
                "MsgType",
                "Content",
                "ArticleCount",
                "Articles",
                "FuncFlag",
                "Title ",
                "Description ",
                "PicUrl",
                "Url",
                "Image"
            }).IndexOf);

            foreach (PropertyInfo propertyInfo in Enumerable.ToList <PropertyInfo>((IEnumerable <PropertyInfo>)Enumerable.OrderBy <PropertyInfo, int>((IEnumerable <PropertyInfo>)entity.GetType().GetProperties(), (Func <PropertyInfo, int>)(p => orderByPropName(p.Name)))))
            {
                string name = propertyInfo.Name;
                if (name == "Articles")
                {
                    XElement xelement = new XElement((XName)"Articles");
                    foreach (Article entity1 in propertyInfo.GetValue((object)entity, (object[])null) as List <Article> )
                    {
                        IEnumerable <XElement> enumerable = EntityHelper.ConvertEntityToXml <Article>(entity1).Root.Elements();
                        xelement.Add((object)new XElement((XName)"item", (object)enumerable));
                    }
                    root.Add((object)xelement);
                }
                else if (name == "Image")
                {
                    XElement xelement = new XElement((XName)"Image");
                    xelement.Add((object)new XElement((XName)"MediaId", (object)new XCData(((Image)propertyInfo.GetValue((object)entity, (object[])null)).MediaId)));
                    root.Add((object)xelement);
                }
                else if (name == "")
                {
                    root.Add((object)new XElement((XName)name, (object)new XCData(propertyInfo.GetValue((object)entity, (object[])null) as string ?? "")));
                }
                else
                {
                    switch (propertyInfo.PropertyType.Name)
                    {
                    case "String":
                        root.Add((object)new XElement((XName)name, (object)new XCData(propertyInfo.GetValue((object)entity, (object[])null) as string ?? "")));
                        break;

                    case "DateTime":
                        root.Add((object)new XElement((XName)name, (object)((DateTime)propertyInfo.GetValue((object)entity, (object[])null)).Ticks));
                        break;

                    case "Boolean":
                        if (name == "FuncFlag")
                        {
                            root.Add((object)new XElement((XName)name, (bool)propertyInfo.GetValue((object)entity, (object[])null) ? (object)"1" : (object)"0"));
                            break;
                        }
                        else
                        {
                            goto default;
                        }

                    case "ResponseMsgType":
                        root.Add((object)new XElement((XName)name, (object)propertyInfo.GetValue((object)entity, (object[])null).ToString().ToLower()));
                        break;

                    case "Article":
                        root.Add((object)new XElement((XName)name, (object)propertyInfo.GetValue((object)entity, (object[])null).ToString().ToLower()));
                        break;

                    default:
                        root.Add((object)new XElement((XName)name, propertyInfo.GetValue((object)entity, (object[])null)));
                        break;
                    }
                }
            }
            return(xdocument);
        }