473,545 Members | 2,577 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 7741


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
2000
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...
7
6044
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...
1
5659
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...
1
2996
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
1733
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...
1
2109
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...
7
11768
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...
13
3665
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
3512
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
7425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7935
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...
0
7780
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...
0
6009
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3479
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...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
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
0
734
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...

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.