473,807 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7757


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>Armstron g</rider>
<rider>Pantan i</rider>
<rider>Ullric h</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("usag e: prog filename tagname position");
}
else {
string tagName = args[1];
string position = args[2];
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(args[0]);
XmlNode node = xmlDocument.Sel ectSingleNode("//" + 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>Armstron g</rider>
<rider>Pantan i</rider>
<rider>Ullric h</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("usag e: prog filename tagname position");
}
else {
string tagName = args[1];
string position = args[2];
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(args[0]);
XmlNode node = xmlDocument.Sel ectSingleNode("//" + 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*******@quin npumps.com> wrote in message
news:16******** *************** ***@posting.goo gle.com...
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>f oo</cont></rec>
<rec id="a2"><cont>b ar</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 IVBSAXContentHa ndler 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
2023
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: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
7
6069
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
1
5685
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 Fortran to read those files and compile this Fortran extension using F2PY. Now that it seems that no possible combinations of Fortran/C compilers actually *work* with Python 2.4 on Windows XP, I was trying to translate the Fortran subroutine to...
1
3012
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> #include <fstream.h> #include <string.h> class Patient {
2
1749
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 to write a program in C++ that would allow the user to enter student information (matriculation number, name, status and mark), which was then stored in an array. They could then search it or list it. Everything was woking (and still is) up to here....
1
2121
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 know why is now outputing what is in the file, this is the program A file called Stock.txt contains information regarding the 9 different stock items carried by FUNNY STUFF RETAIL INC. The file is a repeating sequence of three records such that the...
7
11798
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) Your first task is to write a program which reads this file into two parallel arrays in memory. One array contains the titles, and the other array contains the authors. The arrays are 'parallel' in the sense that the n-th element of the authors...
13
3716
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 consists of structures of type---- struct record { int acountnum; char name; float value;
6
3535
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
10626
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6879
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.