Reputation: 463
I have an XML file in which I have to find the number of occurrences of a word i the XML file. Consider, I have a sample XML file as below
<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 = "[email protected]">
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 = "[email protected]">
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. How is that possible using C# code?
Thanks in advance!
Upvotes: 0
Views: 930
Reputation: 34421
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. To get proper results use 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;
}
}
}
Upvotes: 0
Reputation: 1368
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.
But a more interesting problem is to find all planes whose Color property is Gold :)
(oh I forgot to ask about case sensitivity, but you can specify that in the 2nd parameter to Regex.Matches)
Upvotes: 1