internal static ImageInfo[] FindImgs(
            string htmlCode)
        {
            var r =
                new SgmlReader
            {
                DocType     = @"HTML",
                InputStream = new StringReader(htmlCode)
            };
            var al = new List <ImageInfo>();

            //find <img src=""
            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element)
                {
                    if (String.Compare(r.Name, @"img", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (r.HasAttributes)
                        {
                            var ii = new ImageInfo();

                            while (r.MoveToNextAttribute())
                            {
                                switch (r.Name.ToLowerInvariant())
                                {
                                case @"src":
                                    ii.Source = r.Value;
                                    break;

                                case @"width":
                                    ii.Width = ConvertHelper.ToInt32(r.Value);
                                    break;

                                case @"height":
                                    ii.Height = ConvertHelper.ToInt32(r.Value);
                                    break;
                                }
                            }

                            // --

                            if (!String.IsNullOrEmpty(ii.Source))
                            {
                                al.Add(ii);
                            }
                        }
                    }
                }
            }

            return(al.ToArray());
        }
Exemplo n.º 2
0
        public void Test_MoveToNextAttribute()
        {
            // Make sure we can do MoveToElement after reading multiple attributes.
            var r = new SgmlReader {
                InputStream = new StringReader("<test id='10' x='20'><a/><!--comment-->test</test>")
            };

            Assert.IsTrue(r.Read());
            while (r.MoveToNextAttribute())
            {
                _log.Debug(r.Name);
            }
            if (r.MoveToElement())
            {
                _log.Debug(r.ReadInnerXml());
            }
        }
Exemplo n.º 3
0
        void RegressionTest1()
        {
            // Make sure we can do MoveToElement after reading multiple attributes.
            SgmlReader r = new SgmlReader();

            r.InputStream = new StringReader("<test id='10' x='20'><a/><!--comment-->test</test>");
            if (r.Read())
            {
                while (r.MoveToNextAttribute())
                {
                    Trace.WriteLine(r.Name);
                }
                if (r.MoveToElement())
                {
                    Trace.WriteLine(r.ReadInnerXml());
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Finds the images.
        /// </summary>
        /// <param name="htmlCode">The HTML code.</param>
        /// <returns></returns>
        internal static string[] FindImgs(
            string htmlCode)
        {
            var r =
                new SgmlReader
            {
                DocType     = @"HTML",
                InputStream = new StringReader(htmlCode)
            };
            var al = new List <string>();

            //find <img src=""
            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element)
                {
                    if (string.Compare(r.Name, @"img", true) == 0)
                    {
                        if (r.HasAttributes)
                        {
                            while (r.MoveToNextAttribute())
                            {
                                if (r.Name.ToLower() == @"src")
                                {
                                    if (!al.Contains(r.Value))
                                    {
                                        al.Add(r.Value);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(al.ToArray());
        }