Esempio n. 1
0
        public void ReadXml(Xml.XmlReader reader)
        {
            bool wasEmpty = reader.IsEmptyElement;

            reader.Read();
            if (wasEmpty)
            {
                return;
            }

            while (reader.NodeType != Xml.XmlNodeType.EndElement)
            {
                string key   = reader.GetAttribute("key");
                string value = reader.GetAttribute("value");
                this.Add((TKey)Convert.ChangeType(key, typeof(TKey)),
                         (TValue)Convert.ChangeType(value, typeof(TValue)));
                reader.Read();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }
Esempio n. 2
0
        public static List <TextElement> Parse(string text, Style style, bool enabled)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new List <TextElement>());
            }

            if (enabled)
            {
                text = ConvertBbCodeToHtml(text);
            }
            else
            {
                text = EscapeXml(text);

                text = text.Replace("\r\n", "<br/>");
                text = text.Replace("\n", "<br/>");

                text = text.Replace("&quot;r&quot;n", "<br/>");
                text = text.Replace("&quot;n", "<br/>");
            }

            var element = new TextElement {
                Font = style.Font, Color = style.TextColor
            };
            var lastElement = new TextElement {
                Font = style.Font, Color = style.TextColor
            };
            var result = new List <TextElement>();

            if (_reader == null)
            {
                _reader = new Xml.XmlReader(text);
            }
            else
            {
                _reader.New(text);
            }

            while (_reader.Read())
            {
                switch (_reader.NodeType)
                {
                case XmlNodeType.Element:

                    switch (_reader.Name.ToLower())
                    {
                    case "control":
                        if (_reader.HasAttributes)
                        {
                            while (_reader.MoveToNextAttribute())
                            {
                                if (_reader.Name.Equals("key"))
                                {
                                    element.Control = _reader.Value;
                                }
                                else if (_reader.Name.Equals("data"))
                                {
                                    // element.Data = reader.Value;
                                }
                            }
                        }

                        result.Add(element);
                        element = new TextElement(element);

                        break;

                    case "font":
                        if (_reader.HasAttributes)
                        {
                            lastElement.Color = element.Color;
                            lastElement.Font  = element.Font;

                            while (_reader.MoveToNextAttribute())
                            {
                                if (_reader.Name.Equals("name"))
                                {
                                    element.Font = _reader.Value;
                                }
                                else if (_reader.Name.Equals("color"))
                                {
                                    var color = element.Color.Value;

                                    if (int.TryParse(_reader.Value, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out color))
                                    {
                                        element.Color = color;
                                    }
                                    else if (int.TryParse(_reader.Value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out color))
                                    {
                                        element.Color = color;
                                    }
                                }
                            }
                        }

                        break;

                    case "a":
                        if (_reader.HasAttributes)
                        {
                            while (_reader.MoveToNextAttribute())
                            {
                                if (_reader.Name.Equals("href"))
                                {
                                    element.Href = _reader.Value;
                                }
                            }
                        }
                        break;

                    case "br":
                        element.Linebreak = true;
                        result.Add(element);

                        element = new TextElement(element);
                        break;

                    default: break;
                    }

                    break;

                case XmlNodeType.EndElement:
                    switch (_reader.Name.ToLower())
                    {
                    case "control":
                        element.Font  = lastElement.Font;
                        element.Color = lastElement.Color;
                        break;

                    case "font":
                        element.Font  = lastElement.Font;
                        element.Color = lastElement.Color;
                        break;

                    case "a":
                        element.Href = null;
                        break;
                    }

                    break;

                case XmlNodeType.Text:
                    if (_reader.Value.Length > 0)
                    {
                        element.Text = UnescapeXml(_reader.Value);
                        result.Add(element);

                        element = new TextElement(element);
                    }
                    break;

                default:
                    break;
                }
            }

            return(result);
        }