/// <summary>设置类型</summary>
        /// <param name="key"></param>
        /// <param name="value">数值</param>
        public void SetItem(String key, Object value)
        {
            if (value == null)
            {
                this[key] = String.Empty;
                return;
            }

            var t = value.GetType();

            if (t.IsValueType || Type.GetTypeCode(t) == TypeCode.String || t == typeof(Object))
            {
                this[key] = value.ToString();
                return;
            }
            else if (value is IEnumerable)
            {
                var data = new ExtendData();
                data.Root = key;
                IEnumerable list = value as IEnumerable;
                Int32 i = 1;
                foreach (var item in list)
                {
                    data["Item" + i++.ToString()] = item.ToString();
                }
                this[key] = data.ToXml();
                if (XmlKeys == null) XmlKeys = new List<String>();
                if (!XmlKeys.Contains(key)) XmlKeys.Add(key);

                return;
            }

            throw new XException(String.Format("不支持的类型{0},键{1},数据{2}", t, key, value));
        }
Exemple #2
0
        /// <summary>转为Xml</summary>
        /// <returns></returns>
        public String ToXml()
        {
            XmlDocument doc      = new XmlDocument();
            String      rootName = Root;

            if (String.IsNullOrEmpty(rootName))
            {
                rootName = "Extend";
            }
            XmlElement root = doc.CreateElement(rootName);

            doc.AppendChild(root);

            if (Data != null && Data.Count > 0)
            {
                foreach (String item in Data.Keys)
                {
                    XmlElement elm = doc.CreateElement(item);
                    if (XmlKeys != null && XmlKeys.Contains(item))
                    {
                        elm.InnerXml = Data[item];
                    }
                    else
                    {
                        elm.InnerText = Data[item];
                    }
                    root.AppendChild(elm);
                }
            }

            return(doc.OuterXml);
        }
        /// <summary>转为Xml</summary>
        /// <returns></returns>
        public String ToXml()
        {
            var doc = new XmlDocument();
            var rootName = Root;
            if (String.IsNullOrEmpty(rootName)) rootName = "Extend";
            var root = doc.CreateElement(rootName);
            doc.AppendChild(root);

            if (Data != null && Data.Count > 0)
            {
                foreach (var item in Data)
                {
                    var elm = doc.CreateElement(item.Key);
                    if (XmlKeys != null && XmlKeys.Contains(item.Key))
                        elm.InnerXml = item.Value;
                    else if (item.Value != null && item.Value.GetTypeCode() == TypeCode.Boolean)
                        elm.InnerText = item.Value.ToString().ToLower();
                    else
                        elm.InnerText = item.Value;
                    root.AppendChild(elm);
                }
            }

            return doc.OuterXml;
        }