Reading the number of records in an XML file

Discussion in 'XML' started by Milo Woodward, Aug 17, 2003.

  1. 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.
    Milo Woodward, Aug 17, 2003
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Aug 18, 2003
    #2
    1. Advertising

  3. 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 <> wrote in message news:<>...
    > 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
    Milo Woodward, Aug 19, 2003
    #3
  4. 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/
    Martin Honnen, Aug 19, 2003
    #4
  5. Milo Woodward

    Andy Fish Guest

    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" <> wrote in message
    news:...
    > 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.
    Andy Fish, Aug 19, 2003
    #5
  6. 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!
    Patrick Gresham, Aug 20, 2003
    #6
  7. nobody <> 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/...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
    Georg Bauhaus, Aug 28, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig
    Replies:
    1
    Views:
    590
    Thomas Kellerer
    Mar 15, 2005
  2. Luke Airig
    Replies:
    0
    Views:
    787
    Luke Airig
    Dec 31, 2003
  3. Replies:
    2
    Views:
    628
    Thomas Matthews
    Feb 27, 2007
  4. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    461
    Ray at
    May 10, 2004
  5. Replies:
    3
    Views:
    655
    Anthony Jones
    Nov 2, 2006
Loading...

Share This Page