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












0














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 = "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.
How is that possible using C# code?



Thanks in advance!










share|improve this question






















  • Is that really what you want though? What if the seller's surname is "Gold", or they live in Gold Beach? And what about if Gold is part of another word (Golden etc.)? What information do you actually need to extract?
    – Dylan Nicholson
    Nov 23 '18 at 8:30












  • Thanks @DylanNicholson. Yes!I want to extract the number of occurrences of a string no matter in which part of word it is.
    – Maha
    Nov 23 '18 at 8:32










  • Read it into an XmlDocument, grab InnerText and search?
    – Sami Kuhmonen
    Nov 23 '18 at 8:34






  • 1




    Ok, what about if Gold is part of the xml tag name, attribute name or comments? If you really don't care, it's irrelevant that it's XML, and you can just read the whole file into a string and use Regex.Matches(source, "Gold").Count
    – Dylan Nicholson
    Nov 23 '18 at 8:36








  • 1




    have you tried an easy regex count?
    – Isitar
    Nov 23 '18 at 8:38
















0














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 = "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.
How is that possible using C# code?



Thanks in advance!










share|improve this question






















  • Is that really what you want though? What if the seller's surname is "Gold", or they live in Gold Beach? And what about if Gold is part of another word (Golden etc.)? What information do you actually need to extract?
    – Dylan Nicholson
    Nov 23 '18 at 8:30












  • Thanks @DylanNicholson. Yes!I want to extract the number of occurrences of a string no matter in which part of word it is.
    – Maha
    Nov 23 '18 at 8:32










  • Read it into an XmlDocument, grab InnerText and search?
    – Sami Kuhmonen
    Nov 23 '18 at 8:34






  • 1




    Ok, what about if Gold is part of the xml tag name, attribute name or comments? If you really don't care, it's irrelevant that it's XML, and you can just read the whole file into a string and use Regex.Matches(source, "Gold").Count
    – Dylan Nicholson
    Nov 23 '18 at 8:36








  • 1




    have you tried an easy regex count?
    – Isitar
    Nov 23 '18 at 8:38














0












0








0







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 = "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.
How is that possible using C# code?



Thanks in advance!










share|improve this question













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 = "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.
How is that possible using C# code?



Thanks in advance!







c# xml






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 8:26









Maha

256




256












  • Is that really what you want though? What if the seller's surname is "Gold", or they live in Gold Beach? And what about if Gold is part of another word (Golden etc.)? What information do you actually need to extract?
    – Dylan Nicholson
    Nov 23 '18 at 8:30












  • Thanks @DylanNicholson. Yes!I want to extract the number of occurrences of a string no matter in which part of word it is.
    – Maha
    Nov 23 '18 at 8:32










  • Read it into an XmlDocument, grab InnerText and search?
    – Sami Kuhmonen
    Nov 23 '18 at 8:34






  • 1




    Ok, what about if Gold is part of the xml tag name, attribute name or comments? If you really don't care, it's irrelevant that it's XML, and you can just read the whole file into a string and use Regex.Matches(source, "Gold").Count
    – Dylan Nicholson
    Nov 23 '18 at 8:36








  • 1




    have you tried an easy regex count?
    – Isitar
    Nov 23 '18 at 8:38


















  • Is that really what you want though? What if the seller's surname is "Gold", or they live in Gold Beach? And what about if Gold is part of another word (Golden etc.)? What information do you actually need to extract?
    – Dylan Nicholson
    Nov 23 '18 at 8:30












  • Thanks @DylanNicholson. Yes!I want to extract the number of occurrences of a string no matter in which part of word it is.
    – Maha
    Nov 23 '18 at 8:32










  • Read it into an XmlDocument, grab InnerText and search?
    – Sami Kuhmonen
    Nov 23 '18 at 8:34






  • 1




    Ok, what about if Gold is part of the xml tag name, attribute name or comments? If you really don't care, it's irrelevant that it's XML, and you can just read the whole file into a string and use Regex.Matches(source, "Gold").Count
    – Dylan Nicholson
    Nov 23 '18 at 8:36








  • 1




    have you tried an easy regex count?
    – Isitar
    Nov 23 '18 at 8:38
















Is that really what you want though? What if the seller's surname is "Gold", or they live in Gold Beach? And what about if Gold is part of another word (Golden etc.)? What information do you actually need to extract?
– Dylan Nicholson
Nov 23 '18 at 8:30






Is that really what you want though? What if the seller's surname is "Gold", or they live in Gold Beach? And what about if Gold is part of another word (Golden etc.)? What information do you actually need to extract?
– Dylan Nicholson
Nov 23 '18 at 8:30














Thanks @DylanNicholson. Yes!I want to extract the number of occurrences of a string no matter in which part of word it is.
– Maha
Nov 23 '18 at 8:32




Thanks @DylanNicholson. Yes!I want to extract the number of occurrences of a string no matter in which part of word it is.
– Maha
Nov 23 '18 at 8:32












Read it into an XmlDocument, grab InnerText and search?
– Sami Kuhmonen
Nov 23 '18 at 8:34




Read it into an XmlDocument, grab InnerText and search?
– Sami Kuhmonen
Nov 23 '18 at 8:34




1




1




Ok, what about if Gold is part of the xml tag name, attribute name or comments? If you really don't care, it's irrelevant that it's XML, and you can just read the whole file into a string and use Regex.Matches(source, "Gold").Count
– Dylan Nicholson
Nov 23 '18 at 8:36






Ok, what about if Gold is part of the xml tag name, attribute name or comments? If you really don't care, it's irrelevant that it's XML, and you can just read the whole file into a string and use Regex.Matches(source, "Gold").Count
– Dylan Nicholson
Nov 23 '18 at 8:36






1




1




have you tried an easy regex count?
– Isitar
Nov 23 '18 at 8:38




have you tried an easy regex count?
– Isitar
Nov 23 '18 at 8:38












2 Answers
2






active

oldest

votes


















1














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)






share|improve this answer





























    0














    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:temptest.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;

    }
    }
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443005%2fcount-the-no-of-occurrences-of-a-word-in-xml-using-c-sharp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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)






      share|improve this answer


























        1














        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)






        share|improve this answer
























          1












          1








          1






          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)






          share|improve this answer












          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)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 8:41









          Dylan Nicholson

          885416




          885416

























              0














              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:temptest.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;

              }
              }
              }





              share|improve this answer


























                0














                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:temptest.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;

                }
                }
                }





                share|improve this answer
























                  0












                  0








                  0






                  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:temptest.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;

                  }
                  }
                  }





                  share|improve this answer












                  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:temptest.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;

                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 14:29









                  jdweng

                  16.8k2717




                  16.8k2717






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443005%2fcount-the-no-of-occurrences-of-a-word-in-xml-using-c-sharp%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo