Click here to Skip to main content
15,914,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<Books Type="Science">

</Books>

<Books Type="Story"> 

</Books>

<Books Type="History">

</Books>


This is a sample text file.
Here I want to count the number of occurrence of the nodes such as Type="Science" ,"Story".

How to do using text reading from txt file
Posted
Comments
[no name] 26-Mar-14 14:02pm    
In addition to the solutions presented here, here could also just refer back to the answers you already have to the same question already asked by you.

You should use XPATH for doing this (you could search on MSDN or Google about XPATH):

C#
string expresion= "/Books[@Type='Science' or @Type='Story']"; //XPATH expression!

XmlDocument xml = new XmlDocument();
xml.LoadXml(filePath); //Your file path!
XmlNodeList nodeList = xml.SelectNodes(expresion);
int n = nodeList.Count;
 
Share this answer
 
v2
You can use below code:

C#
XDocument doc = XDocument.Load(@"C:\XMLFile1.xml");
var query = from ele in doc.Descendants("Books")
            where ele.Attribute("Type").Value == "Science"
            select ele;
Console.WriteLine( query.Count());
Console.Read();


This should solve your problem.

Thanks
 
Share this answer
 
Since this looks like XML, I'd suggest using the XML reading and analysis capabilities already present in .NET.
Look at
LINQ to XML[^]
and
System.Xml.Linq namespace[^]
 
Share this answer
 
You can do it with linq for example:
C#
//first read the text file into a string
string text = //;

string searchTerm1 = "Type=\"Story\"",
       searchTerm2 = "Type=\"Story\"";

//here is the linq query
var matchQuery = from i in text
                 where i.ToLowerInvariant() == searchTerm1.ToLowerInvariant() || 
                       i.ToLowerInvariant() == searchTerm2.ToLowerInvariant()
                 select i;

int count = matchQuery.Count();
 
Share this answer
 
v2
Comments
Matt T Heffron 26-Mar-14 14:06pm    
This approach ignores the structure of the text.
It will count occurrences of "Science" or "Story" anywhere in the file, not just in the context of the Type="..." attributes.
norbitrial 26-Mar-14 14:18pm    
Then you can use I've improved my solution. I've changed the values of the searchTerm1 and the searchTerm2 variables.
Matt T Heffron 26-Mar-14 15:15pm    
A couple more comments:
If the file really is XML then a case-insensitive comparison probably is wrong, at least in part, as XML is case-sensitive.
I still think this is the wrong approach in general.
If dealing with XML text, then treating it as XML is the better strategy. The code will make more sense in the context of the specific problem.
The implementation will be impedance-matched to the problem.
The vocabulary with which the implementation addresses the problem will be the correct vocabulary of the problem domain.
norbitrial 27-Mar-14 4:18am    
I'm completely agree with you, that looks the best strategy for XMLs. I just thought it is a simple text file and not an XML.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900