码迷,mamicode.com
首页 > Windows程序 > 详细

C#控制台基础 在博客备份xml文件中提取所有博文的标题 (正则,流读取)

时间:2017-02-08 10:37:23      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:regular   windows   语言   void   科研   end   item   toe   blocks   

 镇场诗:
    清心感悟智慧语,不着世间名与利。学水处下纳百川,舍尽贡高我慢意。
    学有小成返哺根,愿铸一良心博客。诚心于此写经验,愿见文者得启发。
——————————————————————————————————————————

introduction:

  解析 博客园-博客备份 生成的XML文件,获得所有博文的标题。

 

code:

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Net;
 6 using System.Text;
 7 using System.Text.RegularExpressions;
 8 using System.Threading.Tasks;
 9 
10 
11 namespace ConsoleApplication7
12 {
13     class Program
14     {
15         /// <summary>
16         /// 用于计数的,静态变量便于统计
17         /// </summary>
18         static int num = 1;
19 
20         /// <summary>
21         /// 用多个正则表达式处理一组文件,并返回一个符合正则表达式的MatchCollection列表
22         /// </summary>
23         /// <param name="pathArray">需要读取的一组文件的绝对路径</param>
24         /// <param name="regularExpressionArray">正则表达式组,可以传递多个正则表达式</param>
25         /// <returns>符合正则表达式的MatchCollection列表</returns>
26         static List<MatchCollection> OutPutTitle(string[] pathArray, string[] regularExpressionArray)
27         {
28             string htmlCode;//存储文件内容的字符串
29             List<MatchCollection> matchesList = new List<MatchCollection>(); //存储 用正则表达式筛选后的结果
30             foreach (var path in pathArray)
31             {
32                 //采用流的方式读取,处理大文本做准备
33                 using (StreamReader sReader = new StreamReader(path, Encoding.UTF8))
34                 {
35                     //读到末尾
36                     htmlCode = sReader.ReadToEnd();
37                     //在html源代码中 ‘是 &#39;  所以,要将&#39;替换成‘
38                     htmlCode = htmlCode.Replace(@"&#39;", @"");
39                     htmlCode = htmlCode.Replace(@"&gt;", @">");
40                     foreach (var regularExpression in regularExpressionArray)
41                     {
42                         matchesList.Add(Regex.Matches(htmlCode, regularExpression));
43                     }
44                 }
45             }
46             return matchesList;
47         }
48 
49         /// <summary>
50         /// 接受正则表达式筛选后的结果列表,将所需的内容保存到文件中
51         /// </summary>
52         /// <param name="matchesList">正则表达式筛选后的结果列表</param>
53         /// <param name="saveFileName">新建文件的名字.扩展名</param>
54         static void CreateFile(List<MatchCollection> matchesList, string saveFileName)
55         {
56             using (StreamWriter sWriter = new StreamWriter(saveFileName))
57             {
58                 foreach (MatchCollection matches in matchesList)
59                 {
60                     foreach (Match item in matches)
61                     {
62                         //便于查看输出了些什么内容
63                         Console.WriteLine(num.ToString().PadLeft(3, 0) + " " + item.Groups[1].Value);
64                         //如果没有到末尾,我就读一行写一行
65                         //0->000 1->001 为了对齐好看
66                         sWriter.WriteLine(num.ToString().PadLeft(3, 0) + " " + item.Groups[1].Value);
67                         num++;
68                     }
69                 }
70             }
71         }
72 
73         static void Main(string[] args)
74         {
75             //存储备份文件的  文件夹的绝对路径  ---需要更改
76             string directoryPath = @"C:\Users\Administrator\Desktop\htmlCode";
77             //注:路径是文件夹的,不是文件的
78             
79             //记录生成结果的文件   文件名+类型  ---需要更改
80             string fileName = @"博客园博文标题.txt";
81 
82             List<string> regularExpressionList = new List<string>();
83             string re1 = @"<item><title>(.+)</title>";
84             regularExpressionList.Add(re1);
85             string[] regularExpressionArray = regularExpressionList.ToArray();
86             //只打开指定文件夹下的xml类型文件
87             string[] pathArray = Directory.GetFiles(directoryPath, "*.xml");
88             List<MatchCollection> matchesList = OutPutTitle(pathArray, regularExpressionArray);
89             CreateFile(matchesList, fileName);
90             Console.WriteLine("输出文件成功");
91             Console.ReadKey();
92         }
93     }
94 }
View Code

 

result:

  console:

    技术分享

 

   file:

技术分享

 

博文的数量:

技术分享

对上了,没有遗漏。

 

后记:

  我在百度上搜索 看看别人有没有做过做过类似的事情,一看真有:

链接: http://www.cnblogs.com/zichi/p/cnblogs-backup.html

  

  然后,这个程序分析的是 一个指定文件夹内所有的xml文件。正则表达式也可以提供多个。

    工作流程是:

       提供文件夹的绝对路径,提供一个或者多个正则表达式,添加到正则表达式存储列表中。

       打开文件夹内,第一个xml文件,用第一个正则表达式分析,将结果添加到类型为List<MatchCollection>的变量中,

                     用第二个正则表达式分析,将结果添加到类型为List<MatchCollection>的变量中,直至用完提供的所有正则表达式。

               第二个xml文件,。。。同上了

       然后,将类型为List<MatchCollection>的变量中有用的部分,输出到文件中,进行保存.

 

  注:我是一个新手,就这几十行的代码,写了5个小时,说出来都好笑。不过,第一次分析他人提供的文件,还是很高兴的。

       

——————————————————————————————————————————
博文的精髓,在技术部分,更在镇场一诗。版本:VS2015 系统:Windows 7
C#是优秀的语言,值得努力学习。我是跟随 传智播客\黑马 .Net视频教程学习的。
如果博文的内容有可以改进的地方,甚至有错误的地方,请留下评论,我一定努力改正,争取铸成一个良心博客。
注:此文仅作为科研学习,如果我无意中侵犯了您的权益,请务必及时告知,我会做出改正。

C#控制台基础 在博客备份xml文件中提取所有博文的标题 (正则,流读取)

标签:regular   windows   语言   void   科研   end   item   toe   blocks   

原文地址:http://www.cnblogs.com/jinlingzi/p/6376949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!