[XmlElement("all_count")]
        public long AllCount { get; set; }
        [XmlElement("bulletin")]
        public string Bulletin { get; set; }
//xml这块我不太熟悉,请问我能获取到all_count,bulletin吗?//传入一个已经序列化的类,得到所有的xmlelment属性值,最后拼成一个字符串返回

解决方案 »

  1.   

    可能这块没有做过,描述的不太清楚,我就罗嗦一点吧
    [Serializable]
        public class Shop : TopObject
        {
            public Shop();        [XmlElement("all_count")]
            public long AllCount { get; set; }
            [XmlElement("bulletin")]
            public string Bulletin { get; set; }
            [XmlElement("cid")]
            public long Cid { get; set; }
            [XmlElement("created")]
            public string Created { get; set; }
            [XmlElement("desc")]
            public string Desc { get; set; }
            [XmlElement("modified")]
            public string Modified { get; set; }
            [XmlElement("nick")]
            public string Nick { get; set; }
            [XmlElement("pic_path")]
            public string PicPath { get; set; }
            [XmlElement("remain_count")]
            public long RemainCount { get; set; }
            [XmlElement("shop_score")]
            public ShopScore ShopScore { get; set; }
            [XmlElement("sid")]
            public long Sid { get; set; }
            [XmlElement("title")]
            public string Title { get; set; }
            [XmlElement("used_count")]
            public long UsedCount { get; set; }
        }我想写一个方法,GetStringForObject(Type t)
    假定我传入Shop,最后返回我的结果是all_count,bulletin,cid.......used_count获取这个类的 [XmlElement("created")]的结果,最后拼成一个字符串返回
      

  2.   

    public static string GetStringForObject(Type type)
    {
        string strObject = "";
        PropertyInfo[] pis = type.GetProperties();
        foreach (var pi in pis)
        {
            var attr = pi.GetCustomAttributes(typeof(XmlElementAttribute), true);
            var elem = attr[0] as XmlElementAttribute;
            strObject += elem.ElementName + ",";
        }
        strObject = strObject.TrimEnd(',');
        return strObject;
    }
      

  3.   

    partial class Program
    {
      static void Main()
      {
      PropertyInfo[] peroperties = typeof(A).GetProperties(BindingFlags.Public | BindingFlags.Instance);
      foreach (PropertyInfo property in peroperties)
      {
      object[] objs = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
      if (objs.Length > 0)
      {
      Console.WriteLine("{0}: {1}", property.Name, ((DescriptionAttribute)objs[0]).Description);
      }
      }
      Console.ReadKey();
      }
    }class A
    {
      [Description("X")]
      public string X
      {
      get { return null; }
      }
      [Description("Y")]
      public string Y
      {
      get { return null; }
      }
    }