471,086 Members | 851 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 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 7591
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Boris ©avc | last post: by
3 posts views Thread by Konan | last post: by
3 posts views Thread by Jeremy | last post: by
3 posts views Thread by Anne-Marte | last post: by
reply views Thread by Hareth | last post: by
15 posts views Thread by ruca | last post: by
28 posts views Thread by tlpell | last post: by

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.