473,387 Members | 1,592 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,387 software developers and data experts.

Reading the number of records in an XML file

Does anyone know how to retrieve the number of records an XML file
contains with a vb.net method? I can read through an entire XML file,
import records into SQL Server, etc. However, I don't want to have to
cycle through the entire XML file in order to do this. When I am
importing the XML file, I want to display record X of XX. Any ideas
would be appreciated. Thanks in advance.
Jul 20 '05 #1
6 7727


Milo Woodward wrote:
Does anyone know how to retrieve the number of records an XML file
contains with a vb.net method? I can read through an entire XML file,
import records into SQL Server, etc. However, I don't want to have to
cycle through the entire XML file in order to do this. When I am
importing the XML file, I want to display record X of XX. Any ideas
would be appreciated. Thanks in advance.


You can read the XML into an XmlDocument and then you can use XPath to
find the element you are looking for e.g. with an XML example like this

<?xml version="1.0" encoding="UTF-8"?>
<tdf-winners>
<rider>Armstrong</rider>
<rider>Pantani</rider>
<rider>Ullrich</rider>
<rider>Riis</rider>
<rider>Indurain</rider>
</tdf-winners>

and a C# .NET console application like this

using System.Xml;

public class Test20030818 {
public static void Main (string[] args) {
if (args.Length < 3) {
System.Console.WriteLine("usage: prog filename tagname position");
}
else {
string tagName = args[1];
string position = args[2];
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(args[0]);
XmlNode node = xmlDocument.SelectSingleNode("//" + tagName + "["
+ position + "]");
if (node != null) {
System.Console.WriteLine(node.OuterXml);
}
else {
System.Console.WriteLine("No <" + tagName + "> element found.");
}
}
}
}

you can for instance select the second <rider> element
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
I can't read C# very well - I need to pull the xml file out a
directory (eg: C:\XML). Does your example go to the end of file or
cycle through each line? These files are 200MB +. I appreciate your
help!

Martin Honnen <Ma***********@t-online.de> wrote in message news:<3F**************@t-online.de>...
Milo Woodward wrote:
Does anyone know how to retrieve the number of records an XML file
contains with a vb.net method? I can read through an entire XML file,
import records into SQL Server, etc. However, I don't want to have to
cycle through the entire XML file in order to do this. When I am
importing the XML file, I want to display record X of XX. Any ideas
would be appreciated. Thanks in advance.


You can read the XML into an XmlDocument and then you can use XPath to
find the element you are looking for e.g. with an XML example like this

<?xml version="1.0" encoding="UTF-8"?>
<tdf-winners>
<rider>Armstrong</rider>
<rider>Pantani</rider>
<rider>Ullrich</rider>
<rider>Riis</rider>
<rider>Indurain</rider>
</tdf-winners>

and a C# .NET console application like this

using System.Xml;

public class Test20030818 {
public static void Main (string[] args) {
if (args.Length < 3) {
System.Console.WriteLine("usage: prog filename tagname position");
}
else {
string tagName = args[1];
string position = args[2];
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(args[0]);
XmlNode node = xmlDocument.SelectSingleNode("//" + tagName + "["
+ position + "]");
if (node != null) {
System.Console.WriteLine(node.OuterXml);
}
else {
System.Console.WriteLine("No <" + tagName + "> element found.");
}
}
}
}

you can for instance select the second <rider> element

Jul 20 '05 #3


Milo Woodward wrote:
I can't read C# very well - I need to pull the xml file out a
directory (eg: C:\XML). Does your example go to the end of file or
cycle through each line? These files are 200MB +. I appreciate your
help!


You might want to ask in the dotnet.xml group on news.microsoft.com. My
example is not feasible for files of that size.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4
I'm not really sure what you mean by a 'record'. do you mean the number of
elements, the number of second-level elements, or the number of 'physical'
lines (i.e. line feed characters)?

either way you can't expect any piece of software to be able to tell how
many records are in a file without reading that file to the end, unless you
have the file in a specific format which includes the number of records in
the header.

if you have control over the software that's creating the file, you could
have it record the number of records at the top (or even somewhere else
entirely)

"Milo Woodward" <mw*******@quinnpumps.com> wrote in message
news:16**************************@posting.google.c om...
Does anyone know how to retrieve the number of records an XML file
contains with a vb.net method? I can read through an entire XML file,
import records into SQL Server, etc. However, I don't want to have to
cycle through the entire XML file in order to do this. When I am
importing the XML file, I want to display record X of XX. Any ideas
would be appreciated. Thanks in advance.

Jul 20 '05 #5


I would use count() and node index + 1. While I am not entirely familar
with who to pull the current node index, it can be done. You can use
the count() to get the number if elements based on your select
expression.\

i.e. <xsl:value-of select="count(/ROWSET/ROW)" />

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6
nobody <no****@nowhere.org> wrote:
: Milo Woodward wrote:
:> I can't read C# very well - I need to pull the xml file out a
:> directory (eg: C:\XML). Does your example go to the end of file or
:> cycle through each line? These files are 200MB +. I appreciate your
:> help!
: maybe this could do the job for you:
: http://www.gotdotnet.com/Community/U...8-A20F46A38412

If indeed your XML data have a regular structure, like in
<top>
<rec id="a0"><cont>foo</cont></rec>
<rec id="a2"><cont>bar</cont></rec>
</top>

a SAX parser would be a small and efficient way to count the rec
elements. You just write a callback for the rec element
which increases a counter. The needed SAX parser functionality should be
available in any of today's Windows, in MSXML. Some snippets from
Microsoft texts:
"Dim reader As New SAXXMLReader"
then, "create the ContentHandler by adding a class that implements
the IVBSAXContentHandler interface."
There is introductory material which says that the Visual Basic tools
will assist in most of the mechanical aspects of this, see the MSDN.
HTH, Georg

Jul 20 '05 #7

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

Similar topics

1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
1
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using...
1
by: huibien | last post by:
My program is up and running with no errors whatsoever the problem it is only displaying a single record from the text file instead of all the records that i have done... #include <iostream.h>...
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
1
by: une | last post by:
hey guys, I have this program to do, but I kind of started forst just te read things from the file and then I would go and make the calculation but it is showing some wierd funnky resualts, I dont...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.