简体   繁体   English

使用C#计算xml中一个单词的出现次数

[英]Count the No of Occurrences of a word in xml using c#

I have an XML file in which I have to find the number of occurrences of a word i the XML file. 我有一个XML文件,其中必须查找XML文件中一个单词出现的次数。 Consider, I have a sample XML file as below 考虑一下,我有一个示例XML文件,如下所示

<planes_for_sale>
   <ad>
      <year> 1977 </year>
      <make> &c; </make>
      <model> Skyhawk </model>
      <color> Light blue and white </color>
      <description> New paint, nearly new interior,
            685 hours SMOH, full IFR King avionics </description>
      <price> 23,495 </price>
      <seller phone = "555-222-3333"> Skyway Aircraft </seller>
      <location>
         <city> Rapid City, </city>
         <state> South Dakota </state>
      </location>
   </ad>
   <ad>
      <year> 1965 </year>
      <make> &p; </make>
      <model> Cherokee </model>
      <color> Gold </color>
      <description> 240 hours SMOH, dual NAVCOMs, DME, 
                new Cleveland brakes, great shape </description>
      <seller phone = "555-333-2222"  
              email = "jseller@www.axl.com">
              John Seller </seller>
      <location>
         <city> St. Joseph, </city>
         <state> Missouri </state>
      </location>
   </ad>
    <ad>
      <year> 1968 </year>
      <make> &p; </make>
      <model> Cherokee </model>
      <color> Gold </color>
      <description> 240 hours SMOH, dual NAVCOMs, DME, 
                new Cleveland brakes, great shape </description>
      <seller phone = "555-333-4444"  
              email = "jseller@www.axl.com">
              John Seller </seller>
      <location>
         <city> xxxxx, </city>
         <state> yyyyyy </state>
      </location>
   </ad>
</planes_for_sale>

Now, say I want to check for the number of occurrences of string "Gold" in the xml file. 现在,假设我要检查xml文件中字符串“ Gold”的出现次数。 How is that possible using C# code? 使用C#代码怎么可能?

Thanks in advance! 提前致谢!

Based on what you've asked for, Regex.Matches(File.ReadAllText(myFile), "Gold").Count will do the job, probably more efficiently than anything you can write yourself. 根据您的要求, Regex.Matches(File.ReadAllText(myFile), "Gold").Count可以完成这项工作,可能比您自己编写的任何文件都更有效。 But a more interesting problem is to find all planes whose Color property is Gold :) 但是,更有趣的问题是找到所有Color属性为Gold的飞机:)

(oh I forgot to ask about case sensitivity, but you can specify that in the 2nd parameter to Regex.Matches) (哦,我忘记询问是否区分大小写,但是您可以在Regex.Matches的第二个参数中指定它)

Don't just look for Gold which may be in a person's name (emila address). 不要只寻找可能以人名(金币地址)的黄金。 Your xml has ampersands which are not valid and give errors. 您的xml中的&符无效并且会出错。 To get proper results use xml linq : 要获得正确的结果,请使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("ad").Where(x => ((string)x.Element("color")).Trim() == "Gold").ToList();

            int count = results.Count;

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM