Reading the number of records in an XML file

Milo Woodward

Usenet Archive
Joined
Aug 17, 2003
Messages
2
Reaction score
0
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.
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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
 

Milo Woodward

Usenet Archive
Joined
Aug 17, 2003
Messages
2
Reaction score
0
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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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.
 

Andy Fish

Usenet Archive
Joined
Dec 30, 2002
Messages
605
Reaction score
0
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)
 

Patrick Gresham

Usenet Archive
Joined
Aug 19, 2003
Messages
4
Reaction score
0
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)" />
 
N

nobody

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/UserSamples/Details.aspx?SampleGuid=4B87DD58-D7E9-4509-BCA8-A20F46A38412

200MB? 10 MB/sec ... !

HTH, andreas
 

Georg Bauhaus

Usenet Archive
Joined
Jan 24, 2002
Messages
91
Reaction score
0
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
 

Raj Babar

Usenet Archive
Joined
Aug 29, 2003
Messages
2
Reaction score
0
Hi,
If I understand U right, U want to count the no of records(nodes) in an XML file. Now there R 2 options, U might want to count a specific node or all the nodes.
1) For all the nodes:

Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.preserveWhiteSpace = true
objXML.async = False
if(objXML.Load("C:\ttt.xml")) then
Count = objXML.documentElement.selectNodes("./*").count end if

2) For a specific Node:

Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.preserveWhiteSpace = true
objXML.async = False
if(objXML.Load("C:\ttt.xml")) then
Count = objXML.documentElement.selectNodes(".//" & NodeName).count end if

Is this what U want?

*** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
 

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
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top