472,353 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 7663


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...
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....
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...
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...
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.