Reading the number of records in an XML file

M

Milo Woodward

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.
 
M

Martin Honnen

Milo said:
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
 
M

Milo Woodward

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 said:
Milo said:
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
 
M

Martin Honnen

Milo said:
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.
 
A

Andy Fish

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)
 
P

Patrick Gresham

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)" />
 
G

Georg Bauhaus

: 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/...mpleGuid=4B87DD58-D7E9-4509-BCA8-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top