Re: How to dynamically resize an array?



On 2009-05-11, Mike <unknown@xxxxxxxxxx> wrote:
roidy wrote:
NoOfEpisodes is unknown until the episodes have read in one by one from
the xml file. In the previous version of the program I just gave the
array a large value eg. 40 as it is unlikely any season of a TV series
will have that many episodes. I`m just tidying the program up and it
seemed wasteful to declare an array that large when it probably isn`t
needed. The only other way would be to read the xml file twice, firstly
counting the number of episodes and then declaring the array, and then
read it again and fetch in the episode data.

You should not need to read the episodes twice.

Once you load the XML document, use XPATH query statements to find
elements of the document. XPATH is specifically designed to search
for the "DOM" of the XML documentt to returns a list of elements by
providing it a query statement. In other words, if your XML file has
this:

<?xml version='1.0'?>
<shows>
<series>....</series>
<series>....</series>
<series>....</series>
<series>....</series>
<series>....</series>
<series>....</series>
<series>....</series>
<series>....</series>
<shows>

Than you can use an xpath like

"//shows/series"

to find all the <series> within the </shows> branch.

The XPATH language syntax is rich and the official specification for
XPATH is located here.

http://www.w3.org/TR/xpath


You know, if you are going to take the stupendously stupid way of reading the
file in to memory all at once - instead of streaming it - you might as well
make your live simple and use linq (air code):

dim series = from node in document.descendants("series") select node

And even then, you can always write an extension method and stream the file in
using an XmlTextReader - since this is a readonly forward operation. Of
course, this is a much more complex operation in VB then in C#, because of the
lack of yield return.... But, still doable.

--
Tom Shelton
.



Relevant Pages

  • Re: Flexible __autoload scheme
    ... > Berislav Lopac wrote: ... >> above, or finding in an XML file using XPath and SimpleXML, assuming ... Because array must be iterated through, while a DOM document can be searched ... via XPath by a simple /*/classname (or something; ...
    (comp.lang.php)
  • Re: How to dynamically resize an array?
    ... In the previous version of the program I just gave the array a large value eg. 40 as it is unlikely any season of a TV series will have that many episodes. ... The only other way would be to read the xml file twice, firstly counting the number of episodes and then declaring the array, and then read it again and fetch in the episode data. ... XPATH is specifically designed to search for the "DOM" of the XML documentt to returns a list of elements by providing it a query statement. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to dynamically resize an array?
    ... NoOfEpisodes is unknown until the episodes have read in one by one from the xml file. ... In the previous version of the program I just gave the array a large value eg. 40 as it is unlikely any season of a TV series will have that many episodes. ... The only other way would be to read the xml file twice, firstly counting the number of episodes and then declaring the array, and then read it again and fetch in the episode data. ...
    (microsoft.public.dotnet.languages.vb)
  • RE: writing to a specific point in an array file
    ... Hi Melanieab, ... would be in an XML file rather than a text file and trying to manipulate ... basically writing an empty string over the current row. ... > array file, but I wasn't sure how to replace just one item. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ArgumentException thrown when converting a memorystream to image
    ... the following code will read the contents from a> file and place them into a byte array. ... >> As I read the xml file in .NET, I parse my xml, retrieve this string, and>> try to convert it into an Image object or Bitmap in the following code. ...
    (microsoft.public.dotnet.languages.csharp)

Loading