![]() |
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. |
Re: Reading the number of records in an XML file
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>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 http://JavaScript.FAQTs.com/ |
Re: Reading the number of records in an XML file
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 <Martin.Honnen@t-online.de> wrote in message news:<3F40A18E.9020408@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>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 |
Re: Reading the number of records in an XML file
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/ |
Re: Reading the number of records in an XML file
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" <mwoodward@quinnpumps.com> wrote in message news:1615a5e3.0308170838.2bc10968@posting.google.c om... > 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. |
Re: Reading the number of records in an XML file
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! |
Re: Reading the number of records in an XML file
nobody <nobody@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>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 |
| All times are GMT. The time now is 10:54 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.