示例#1
0
        /// <summary>
        /// 移除其他的文本信息.
        /// </summary>
        /// <param name="html"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private string RemoveOtherText(string html, HtmlReaderConfig config)
        {
            string result = html;

            if (!String.IsNullOrEmpty(config.StartFlag))
            {
                // 如果定义了起始标志. 尝试移除起始标志之前的文本.
                int startIndex = result.IndexOf(config.StartFlag);
                if (startIndex > 0)
                {
                    result = result.Substring(startIndex);
                }
            }

            if (!String.IsNullOrEmpty(config.FinishFlag))
            {
                // 如果定义了结束标志. 尝试移除起结束标志之前的文本.
                int finishIndex = result.IndexOf(config.FinishFlag);
                if (finishIndex > 0)
                {
                    result = result.Substring(0, finishIndex);
                }
            }

            return(result);
        }
示例#2
0
        private T GetValueFromMatch(Match m, HtmlReaderConfig config)
        {
            T result = new T();

            // 对象类型.
            Type fromType = result.GetType();

            // 对象的所有属性.
            PropertyInfo[] fromPropArray = fromType.GetProperties();


            for (int i = 0; i < config.PropertyNameList.Count; i++)
            {
                // 属性名.
                string propertyName = config.PropertyNameList[i];

                PropertyInfo prop = fromPropArray.FirstOrDefault(p => p.Name == propertyName);

                if (prop == null)
                {
                    if (logger.IsWarnEnabled)
                    {
                        logger.WarnFormat("属性 {0} 不存在!", propertyName);
                    }
                    continue;
                }
                if (!prop.CanWrite)
                {
                    if (logger.IsWarnEnabled)
                    {
                        logger.WarnFormat("属性 {0} 不可写入!", propertyName);
                    }
                    continue;
                }


                if (m.Groups.Count <= i)
                {
                    if (logger.IsWarnEnabled)
                    {
                        logger.WarnFormat("属性的数量,大于正则表达式匹配的项目数.");
                    }
                    break;
                }


                // 正则匹配后的文本.
                string regText = m.Groups[i + 1].Value;


                // 为目标对象,设置属性值.
                prop.SetValue(result, regText, null);
            }

            return(result);
        }
示例#3
0
        List <T> IHtmlDataReader <T> .ReadMultiData(string html, HtmlReaderConfig config)
        {
            // 结果列表.
            List <T> resultList = new List <T>();

            // 移除头尾.
            string text = this.RemoveOtherText(html, config);

            // 初始化 正则表达式  忽略大小写
            Regex r = new Regex(config.RegexText, RegexOptions.IgnoreCase | RegexOptions.Singleline);


            // 指定的输入字符串中搜索 Regex 构造函数中指定的正则表达式的第一个匹配项。
            Match m = r.Match(text);

            // 匹配的 计数.
            int matchCount = 0;


            if (logger.IsDebugEnabled)
            {
                logger.DebugFormat("尝试匹配正则表达式:{0} ", config.RegexText);
            }


            while (m.Success)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.DebugFormat("第{0}次匹配!", (++matchCount));

                    for (int i = 0; i < m.Groups.Count; i++)
                    {
                        logger.DebugFormat("m.Groups[ {0} ] = {1}", i, m.Groups[i]);
                    }
                }

                T result = GetValueFromMatch(m, config);

                // 加入结果列表.
                resultList.Add(result);

                m = m.NextMatch();
            }



            // 返回.
            return(resultList);
        }
        private void btnProcess_Click(object sender, EventArgs e)
        {
            string[] propNames = this.txtPropertyName.Text.Split(',');

            HtmlReaderConfig config = new HtmlReaderConfig();

            config.RegexText        = this.txtReg.Text;
            config.PropertyNameList = new List <string>(propNames);



            var result = reader.ReadMultiData(this.txtHtml.Text, config);

            string jsonString = JsonConvert.SerializeObject(result);

            this.txtResult.Text = jsonString;
        }
示例#5
0
        T IHtmlDataReader <T> .ReadSingleData(string html, HtmlReaderConfig config)
        {
            string text = this.RemoveOtherText(html, config);

            // 初始化 正则表达式  忽略大小写
            Regex r = new Regex(config.RegexText, RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // 指定的输入字符串中搜索 Regex 构造函数中指定的正则表达式的第一个匹配项。
            Match m = r.Match(text);


            if (logger.IsDebugEnabled)
            {
                logger.DebugFormat("尝试匹配正则表达式:{0} ", config.RegexText);
            }

            if (m.Success)
            {
                T result = GetValueFromMatch(m, config);
                return(result);
            }

            return(default(T));
        }