473,503 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7723
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
2200
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
1992
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
3231
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
6006
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
295
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
4734
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
4193
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
2240
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
3173
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
7207
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
7095
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
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
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,...
1
5026
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...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.