473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# -> Read lines from XML file

How can I read lines from an XML file. & add them to a counter.

If possible, words between <page> <\page> is considered as +1 to my counter.

if it isnt possible, I would atlest like to read until the word "explorer"
then add it (+1) to my counter, then read next word, and so on.
Example File:

<?xml version="1.0" encoding="utf-8" ?>
- <Site>
<page>Games - Microsoft Internet Explorer</page>
<page>Cars - Microsoft Internet Explorer</page>
<page>The Biggest And The Best! - Microsoft Internet Explorer</page>
<page>AutoWhatever - Microsoft Internet Explorer</page>
<\Site>
I DONT want to add the following "words" to my counter:

<?xml version="1.0" encoding="utf-8" ?>
- <Site><\Site>
or <page></page>


Nov 16 '05 #1
5 7717
Hello Hareth,
First of all, you shouldn't think about reading "lines" from XML. The whole
idea behind XML is that it is deliniated by element tags, not newlines. I
would use the XMLDocument object to access the nodes:

XmlDocument doc = new XmlDocument();
doc.Load(sPath);

//Then I'd do something like (off the top of my head here):

XmlNodeList nlPages = doc.SelectNodes("//page");

int iCount = nlPages.Count;

It might be faster to use a SAX parser, but I doubt it would be significant.
Tom Clement
Apptero, Inc.
"Hareth" <ab******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
How can I read lines from an XML file. & add them to a counter.

If possible, words between <page> <\page> is considered as +1 to my
counter.

if it isnt possible, I would atlest like to read until the word "explorer"
then add it (+1) to my counter, then read next word, and so on.
Example File:

<?xml version="1.0" encoding="utf-8" ?>
- <Site>
<page>Games - Microsoft Internet Explorer</page>
<page>Cars - Microsoft Internet Explorer</page>
<page>The Biggest And The Best! - Microsoft Internet Explorer</page>
<page>AutoWhatever - Microsoft Internet Explorer</page>
<\Site>
I DONT want to add the following "words" to my counter:

<?xml version="1.0" encoding="utf-8" ?>
- <Site><\Site>
or <page></page>

Nov 16 '05 #2
Hello Hareth,
First of all, you shouldn't think about reading "lines" from XML. The whole
idea behind XML is that it is deliniated by element tags, not newlines. I
would use the XMLDocument object to access the nodes:

XmlDocument doc = new XmlDocument();
doc.Load(sPath);

//Then I'd do something like (off the top of my head here):

XmlNodeList nlPages = doc.SelectNodes("//page");

int iCount = nlPages.Count;

It might be faster to use a SAX parser, but I doubt it would be significant.
Tom Clement
Apptero, Inc.
"Hareth" <ab******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
How can I read lines from an XML file. & add them to a counter.

If possible, words between <page> <\page> is considered as +1 to my
counter.

if it isnt possible, I would atlest like to read until the word "explorer"
then add it (+1) to my counter, then read next word, and so on.
Example File:

<?xml version="1.0" encoding="utf-8" ?>
- <Site>
<page>Games - Microsoft Internet Explorer</page>
<page>Cars - Microsoft Internet Explorer</page>
<page>The Biggest And The Best! - Microsoft Internet Explorer</page>
<page>AutoWhatever - Microsoft Internet Explorer</page>
<\Site>
I DONT want to add the following "words" to my counter:

<?xml version="1.0" encoding="utf-8" ?>
- <Site><\Site>
or <page></page>

Nov 16 '05 #3
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(pathAndFilename);

int count = 0;
XmlNodeList pageNodes = xmlDoc.SelectNodes("*/page");
foreach(XmlNode node in pageNodes)
{
if(node.InnerText.Length > 0)
count++;
}

--Liam.

"Hareth" <ab******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
How can I read lines from an XML file. & add them to a counter.

If possible, words between <page> <\page> is considered as +1 to my counter.
if it isnt possible, I would atlest like to read until the word "explorer"
then add it (+1) to my counter, then read next word, and so on.
Example File:

<?xml version="1.0" encoding="utf-8" ?>
- <Site>
<page>Games - Microsoft Internet Explorer</page>
<page>Cars - Microsoft Internet Explorer</page>
<page>The Biggest And The Best! - Microsoft Internet Explorer</page>
<page>AutoWhatever - Microsoft Internet Explorer</page>
<\Site>
I DONT want to add the following "words" to my counter:

<?xml version="1.0" encoding="utf-8" ?>
- <Site><\Site>
or <page></page>

Nov 16 '05 #4
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(pathAndFilename);

int count = 0;
XmlNodeList pageNodes = xmlDoc.SelectNodes("*/page");
foreach(XmlNode node in pageNodes)
{
if(node.InnerText.Length > 0)
count++;
}

--Liam.

"Hareth" <ab******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
How can I read lines from an XML file. & add them to a counter.

If possible, words between <page> <\page> is considered as +1 to my counter.
if it isnt possible, I would atlest like to read until the word "explorer"
then add it (+1) to my counter, then read next word, and so on.
Example File:

<?xml version="1.0" encoding="utf-8" ?>
- <Site>
<page>Games - Microsoft Internet Explorer</page>
<page>Cars - Microsoft Internet Explorer</page>
<page>The Biggest And The Best! - Microsoft Internet Explorer</page>
<page>AutoWhatever - Microsoft Internet Explorer</page>
<\Site>
I DONT want to add the following "words" to my counter:

<?xml version="1.0" encoding="utf-8" ?>
- <Site><\Site>
or <page></page>

Nov 16 '05 #5
"Hareth" <ab******@hotmail.com> wrote in
news:#$**************@TK2MSFTNGP09.phx.gbl:
How can I read lines from an XML file. & add them to a counter.

If possible, words between <page> <\page> is considered as +1 to my
counter.

if it isnt possible, I would atlest like to read until the word
"explorer" then add it (+1) to my counter, then read next word, and so
on.
Example File: Here is an example bit of code that you might be able to adapt:

--------CODE FRAGMENT--------------------------
string xmlpath = "virtualpath";

System.Xml.XmlTextReader myXmlReader;
myXmlReader = new XmlTextReader(xmlpath + "/xmlboard.xml");

while (myXmlReader.Read())
{
if (myXmlReader.NodeType==System.Xml.XmlNodeType.Elem ent)
{
switch ((string) myXmlReader.Name) {
case "page":
counter++;
break;
// other case tests...
// ...
default:
break;
}
}
}
myXmlReader.Close();
----END CODE FRAGMENT---------------------

Bob Dickow


<?xml version="1.0" encoding="utf-8" ?>
- <Site>
<page>Games - Microsoft Internet Explorer</page>
<page>Cars - Microsoft Internet Explorer</page>
<page>The Biggest And The Best! - Microsoft Internet Explorer</page>
<page>AutoWhatever - Microsoft Internet Explorer</page>
<\Site>

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Boris ©avc | last post by:
How do I read data from file with delimiters (for instance A;B;ccc;S45A;UU)? I'd like to write that data to MYSQL table... Thanks for the help, Boris Savc
3
by: Konan | last post by:
Pardon the simple question, but I have just begun to learn PHP. So far so good - all the examples in my books actually work. One thing that none of them address is how to read a file of strings...
3
by: Jeremy | last post by:
I have a most aggravating problem. I don't understand what is causing readlines() not to read all the lines in the file. I have the following syntax: # some initial stuff XS =...
3
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
0
by: Hareth | last post by:
How can I read lines from an XML file. & add them to a counter. If possible, words between <page> <\page> is considered as +1 to my counter. if it isnt possible, I would atlest like to read...
15
by: ruca | last post by:
Hi, Can I read a .TXT File to a DataSet? How can I do that? I want to read his lines to a DropDownList. This lines are the names of employees that I export from an application that I have. I...
3
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another...
0
by: Vader | last post by:
I am new to this forum. Thanks in advance for and help. The following is what I am looking for: 1. I need help with VB code to open a MS Word (.doc) file. 2. Read lines from the MS Word (.doc)...
28
by: tlpell | last post by:
Hey, read some tips/pointers on PHP.net but can't seem to solve this problem. I have a php page that reads the contents of a file and then displays the last XX lines of the file. Problem is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.