Ejemplo n.º 1
0
        /// <summary>
        /// Converts inputValue which is xml to List of RelatedLink
        /// </summary>
        /// <param name="inputValue">Input value (for example string xml)</param>
        /// <returns>
        /// List of RelatedLink created from input xml or empty list if there are no links
        /// </returns>
        public object ConvertValueWhenRead(object inputValue)
        {
            // example of input value: <links><link title="google" link="http://www.google.com" type="external" newwindow="0" /></links>
            string inputString = Convert.ToString(inputValue);

            try
            {
                List <RelatedLink> retVal = new List <RelatedLink>();

                if (!string.IsNullOrEmpty(inputString))
                {
                    if (!Util.IsUmbraco700OrHigher())
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml((string)inputString);

                        foreach (XmlNode node in doc.SelectNodes("links/link"))
                        {
                            RelatedLink rl = new RelatedLink();

                            rl.Title     = node.Attributes["title"].Value;
                            rl.NewWindow = node.Attributes["newwindow"].Value == "0" ? false : true;

                            switch (node.Attributes["type"].Value)
                            {
                            case "external":
                                rl.Type = RelatedLink.RelatedLinkType.External;
                                rl.Url  = node.Attributes["link"].Value;
                                break;

                            case "internal":     // points to some node so Url is nodeid
                                rl.Type          = RelatedLink.RelatedLinkType.Internal;
                                rl.RelatedNodeId = int.Parse(node.Attributes["link"].Value);
                                rl.Url           = new Node((int)rl.RelatedNodeId).NiceUrl;
                                break;

                            case "media":
                                rl.Type          = RelatedLink.RelatedLinkType.Media;
                                rl.RelatedNodeId = int.Parse(node.Attributes["link"].Value);
                                rl.Url           = Util.GetMediaUrlById((int)rl.RelatedNodeId);
                                break;
                            }

                            retVal.Add(rl);
                        }
                    }
                    else
                    {
                        retVal = (List <RelatedLink>)JsonConvert.DeserializeObject <List <RelatedLink> >(inputString);
                    }
                }

                return(retVal);
            }
            catch (Exception exc)
            {
                throw new Exception(string.Format("Cannot convert '{0}' to List<RelatedLink>. Error: {1}",
                                                  inputString, exc.Message));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts inputValue which is xml to List of RelatedLink
        /// </summary>
        /// <param name="inputValue">Input value (for example string xml)</param>
        /// <returns>
        /// List of RelatedLink created from input xml or empty list if there are no links
        /// </returns>
        public object ConvertValueWhenRead(object inputValue)
        {
            // example of input value: <links><link title="google" link="http://www.google.com" type="external" newwindow="0" /></links>
            string inputString = Convert.ToString(inputValue);
            try
            {
                List<RelatedLink> retVal = new List<RelatedLink>();

                if (!string.IsNullOrEmpty(inputString))
                {
                    if (!Util.IsUmbraco700OrHigher())
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml((string)inputString);

                        foreach (XmlNode node in doc.SelectNodes("links/link"))
                        {
                            RelatedLink rl = new RelatedLink();

                            rl.Title = node.Attributes["title"].Value;
                            rl.NewWindow = node.Attributes["newwindow"].Value == "0" ? false : true;

                            switch (node.Attributes["type"].Value)
                            {
                                case "external":
                                    rl.Type = RelatedLink.RelatedLinkType.External;
                                    rl.Url = node.Attributes["link"].Value;
                                    break;
                                case "internal": // points to some node so Url is nodeid
                                    rl.Type = RelatedLink.RelatedLinkType.Internal;
                                    rl.RelatedNodeId = int.Parse(node.Attributes["link"].Value);
                                    rl.Url = new Node((int)rl.RelatedNodeId).NiceUrl;
                                    break;
                                case "media":
                                    rl.Type = RelatedLink.RelatedLinkType.Media;
                                    rl.RelatedNodeId = int.Parse(node.Attributes["link"].Value);
                                    rl.Url = Util.GetMediaUrlById((int)rl.RelatedNodeId);
                                    break;
                            }

                            retVal.Add(rl);
                        }
                    }
                    else
                    {
                        retVal = (List<RelatedLink>)JsonConvert.DeserializeObject<List<RelatedLink>>(inputString);
                    }
                }

                return retVal;
            }
            catch (Exception exc)
            {
                throw new Exception(string.Format("Cannot convert '{0}' to List<RelatedLink>. Error: {1}",
                    inputString, exc.Message));
            }
        }