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