Beispiel #1
0
        /// <summary>
        /// Serializes this state into a number of formats
        /// </summary>
        /// <param name="format">The format desired</param>
        /// <returns>A serialized string</returns>
        public string Serialize(UrlPickerDataFormat format)
        {
            // Serialized return string
            string serializedData;

            // Get text-strings (for XML/CSV)
            string mode      = this.Mode.ToString();
            string newWindow = this.NewWindow.ToString();
            string nodeId    = ((!this.NodeId.HasValue)
                 ? ""
                 : this.NodeId.GetValueOrDefault().ToString());
            string linkTitle = string.IsNullOrEmpty(this.Title)
                ? ""
                : this.Title;
            string url = string.IsNullOrEmpty(this.Url)
                ? ""
                : this.Url;;

            switch (format)
            {
            case UrlPickerDataFormat.Xml:

                serializedData = new XElement("url-picker",
                                              new XAttribute("mode", mode),
                                              new XElement("new-window", newWindow),
                                              new XElement("node-id", nodeId),
                                              new XElement("url", url),
                                              new XElement("link-title", linkTitle)
                                              ).ToString();

                break;

            case UrlPickerDataFormat.Csv:
                // Making sure to escape commas:
                serializedData = mode + "," +
                                 newWindow + "," +
                                 nodeId + "," +
                                 url.Replace(",", "&#45;") + "," +
                                 linkTitle.Replace(",", "&#45;");

                break;

            case UrlPickerDataFormat.Json:

                var jss = new JavaScriptSerializer();
                serializedData = jss.Serialize(this);

                break;

            default:
                throw new NotImplementedException();
            }

            // Return empty if no serialized data set
            return(serializedData);
        }
        /// <summary>
        /// Serializes this state into a number of formats
        /// </summary>
        /// <param name="format">The format desired</param>
        /// <param name="includeEmptyItems">if set to <c>true</c> [include empty items].</param>
        /// <returns>A serialized string</returns>
        public string Serialize(UrlPickerDataFormat format, bool includeEmptyItems = true)
        {
            // Serialized return string
            string serializedData = string.Empty;

            switch (format)
            {
                case UrlPickerDataFormat.Xml:

                    var xmlElement = new XElement("multi-url-picker");

                    foreach (var item in Items)
                    {
                        if (includeEmptyItems || !string.IsNullOrEmpty(item.Url))
                        {
                            var serializedItem = item.Serialize(UrlPickerDataFormat.Xml);
                            if (!string.IsNullOrEmpty(serializedItem))
                            {
                                xmlElement.Add(XElement.Parse(serializedItem));
                            }
                        }
                    }

                    serializedData = xmlElement.ToString();

                    break;

                case UrlPickerDataFormat.Csv:

                    foreach (var item in Items)
                    {
                        if (includeEmptyItems || !string.IsNullOrEmpty(item.Url))
                        {
                            var serializedItem = item.Serialize(UrlPickerDataFormat.Csv);

                            if (!string.IsNullOrEmpty(serializedItem))
                            {
                                serializedData += serializedItem + "\n";
                            }
                        }
                    }

                    break;

                case UrlPickerDataFormat.Json:

                    if (!includeEmptyItems)
                    {
                        // Bit of a cheat to remove empty items - serializes and deserializes to clone the object
                        var clone = MultiUrlPickerState.Deserialize(this.Serialize(UrlPickerDataFormat.Xml, true));

                        // Remove empty items
                        clone.Items = clone.Items.Where(x => !string.IsNullOrEmpty(x.Url));

                        var jss = new JavaScriptSerializer();
                        serializedData = jss.Serialize(clone);
                    }
                    else
                    {
                        var jss = new JavaScriptSerializer();
                        serializedData = jss.Serialize(this);
                    }

                    break;

                default:
                    throw new NotImplementedException();
            }

            return serializedData;
        }
        /// <summary>
        /// Serializes this state into a number of formats
        /// </summary>
        /// <param name="format">The format desired</param>
        /// <returns>A serialized string</returns>
        public string Serialize(UrlPickerDataFormat format)
        {
            // Serialized return string
            string serializedData;

            // Get text-strings (for XML/CSV)
            string mode = this.Mode.ToString();
            string newWindow = this.NewWindow.ToString();
            string nodeId = ((!this.NodeId.HasValue)
                 ? ""
                 : this.NodeId.GetValueOrDefault().ToString());
            string linkTitle = string.IsNullOrEmpty(this.Title)
                ? ""
                : this.Title;
            string url = string.IsNullOrEmpty(this.Url)
                ? ""
                : this.Url; ;

            switch (format)
            {
                case UrlPickerDataFormat.Xml:

                    serializedData = new XElement("url-picker",
                                        new XAttribute("mode", mode),
                                        new XElement("new-window", newWindow),
                                        new XElement("node-id", nodeId),
                                        new XElement("url", url),
                                        new XElement("link-title", linkTitle)
                                    ).ToString();

                    break;

                case UrlPickerDataFormat.Csv:
                    // Making sure to escape commas:
                    serializedData = mode + "," +
                                        newWindow + "," +
                                        nodeId + "," +
                                        url.Replace(",", "&#45;") + "," +
                                        linkTitle.Replace(",", "&#45;");

                    break;

                case UrlPickerDataFormat.Json:

                    var jss = new JavaScriptSerializer();
                    serializedData = jss.Serialize(this);

                    break;

                default:
                    throw new NotImplementedException();
            }

            // Return empty if no serialized data set
            return serializedData;
        }
        /// <summary>
        /// Serializes this state into a number of formats
        /// </summary>
        /// <param name="format">The format desired</param>
        /// <param name="includeEmptyItems">if set to <c>true</c> [include empty items].</param>
        /// <returns>A serialized string</returns>
        public string Serialize(UrlPickerDataFormat format, bool includeEmptyItems = true)
        {
            // Serialized return string
            string serializedData = string.Empty;

            switch (format)
            {
            case UrlPickerDataFormat.Xml:

                var xmlElement = new XElement("multi-url-picker");

                foreach (var item in Items)
                {
                    if (includeEmptyItems || !string.IsNullOrEmpty(item.Url))
                    {
                        var serializedItem = item.Serialize(UrlPickerDataFormat.Xml);
                        if (!string.IsNullOrEmpty(serializedItem))
                        {
                            xmlElement.Add(XElement.Parse(serializedItem));
                        }
                    }
                }

                serializedData = xmlElement.ToString();

                break;

            case UrlPickerDataFormat.Csv:

                foreach (var item in Items)
                {
                    if (includeEmptyItems || !string.IsNullOrEmpty(item.Url))
                    {
                        var serializedItem = item.Serialize(UrlPickerDataFormat.Csv);

                        if (!string.IsNullOrEmpty(serializedItem))
                        {
                            serializedData += serializedItem + "\n";
                        }
                    }
                }

                break;

            case UrlPickerDataFormat.Json:

                if (!includeEmptyItems)
                {
                    // Bit of a cheat to remove empty items - serializes and deserializes to clone the object
                    var clone = MultiUrlPickerState.Deserialize(this.Serialize(UrlPickerDataFormat.Xml, true));

                    // Remove empty items
                    clone.Items = clone.Items.Where(x => !string.IsNullOrEmpty(x.Url));

                    var jss = new JavaScriptSerializer();
                    serializedData = jss.Serialize(clone);
                }
                else
                {
                    var jss = new JavaScriptSerializer();
                    serializedData = jss.Serialize(this);
                }

                break;

            default:
                throw new NotImplementedException();
            }

            return(serializedData);
        }