使用c#搜索文件

时间:2012-07-05 10:57:23

标签: c# c#-4.0

我有一个文件并搜索多个值,即我需要搜索{Name,Class}并将其存储在文件的列表或字典中。我需要使用c#。

实施例。 名称:“ABC” 分类:“第五” 由于表现不佳,他的考试失败了。 名称:“CYS” 等级:9 由于他的出色表现,他通过了考试。 等级:10 无需评论。

         string Filename = @"ClassInfo.fo";
         XmlTextReader reader = new XmlTextReader(Filename);
        List<string> xmlValue = new List<string>();
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Text: //Display the text in each element.
                    Console.WriteLine(reader.Value);
                    xmlValue.Add(reader.Value);
                    break;
            }
        }
         string searchString = "Name :"

         var foundIndices = new List<int>(xmlValue.Count);
         int countValue=0;

         Collection<PdfContent> pdfContent = new Collection<PdfContent>();
        for (int i = 0; i < xmlValue.Count; i++)
        {
            if (xmlValue[i] == searchString)
            {
                foundIndices.Add(i);

                pdfContent.Add(new PdfContent
                {
                    Name= xmlValue[indexOfName],
                    Class=xmlValue[indexOfClass]

                });

            }
        }

1 个答案:

答案 0 :(得分:0)

在读取文件的字符串变量

中的所有文本后,请使用正则表达式匹配模式

正则表达式的示例:http://www.regular-expressions.info/examples.html