XML Parser coding help

O

OpticTygre

Hey folks,

I'm wondering if I'm doing this the best way...

I want to take an XML file with only Parameters in the nodes, ie: <MyTag
Path='C:\SomePath' FileType='TXT'/> or <MyTag Value1='1' Value2='0'
Value3='6' Value4='1234'/> and capture all the values into an arraylist.
One note to add: The XML files passed into this procedure will have a
static amount of Parameter values, meaning if one tag in the document has 4
values, they all will have 4 values. However, the number of parameter
values may change between XML documents, meaning one document could have an
unknown amount of tags with 4 parameter values each, and another an unknown
amount of tags with 2 parameter values each. I hope this makes sense.

Just curious if there was a better way to do it than this...

Private Function ReadXMLFile(ByVal FilePath As String) As ArrayList(,)
Dim objDocument As New XmlDocument
Dim reader As XmlNodeReader
Dim ArrValues(0, 0) As ArrayList
Dim iCounter As Integer = 0
Dim x As Integer

objDocument.Load(FilePath)
reader = New XmlNodeReader(objDocument)

While reader.Read
If reader.IsStartElement Then
If reader.HasAttributes Then
ReDim Preserve ArrValues(iCounter,
reader.AttributeCount - 1)
For x = 0 To reader.AttributeCount - 1
reader.MoveToAttribute(x)
ArrValues(iCounter, x).Add(reader.Value)
Next
iCounter += 1
End If
End If
End While

Return ArrValues

End Function
 
C

Cor Ligthert

OpticTygre,

There is one thing in your design on which almost every regular in this
newsgroup will probably direct give a reaction.

You use a redim on a two dimensional array of arraylist.

An arraylist is ofcours a small object, however remember that a redim will
forever create everytime a complete new array.

I would use a dataset with one or more datatables. The problem is than that
your XML file will be showed with elements only and no attributes. However
that I don't see as a problem.

When you don't want than you can create your own collection. What is as well
very easy to create using collectionbase.

http://msdn.microsoft.com/library/d...systemcollectionscollectionbaseclasstopic.asp

However I prever the dataset.

Cor
 
J

Jason Knott

Hi Cor,

Thanks for the reply. The only problem with creating my own collection
is that it will still have to be dynamic, as the size of the array will
change depending on the XML file read. Meaning, I'll still have to use
ReDim. Perhaps a datatable is the way to go. I should still be able to
dynamically update the size of the table without much problem using the
reader.AttributeCount, correct? Then just pass the datatable back
through the function at the end? I haven't used datatables or datasets
yet, so I'm not sure exactly how it would work.

-Jason
 
C

Cor Ligthert

Jason,

The only one that use the redim is the fixed array.

For the collection did you look at the sample that is inside the page.
You see there functions as list.remove, list.add etc.

For the datast it is simple, this written in this message so watch typos.

\\\
dim ds as new dataset
dim dt as new datatable
ds.tables.add(dt)
dt.columns.add(myfirstcolumn)
dt.columns.add(mysecondcolumn)
///
Use as (there are a lot of other ways for this)
ds.tables(0).rows.add(ds.tables(0).newrow)
ds.tables(0).rows(0)(0) = "myfirsttext"
ds.tables(0).rows(0)(1) = "mysecondtext"
///
this 3 rows above could have written as example as well as
\\\
ds.Tables(0).LoadDataRow(New object() {"myfirsttext",mysecondtext"}, true);
///

And than to write it
\\\
ds.writexml("path")
///
and to read it
\\\
ds.readxml("path")
////
I hope this helps,

Cor
 
J

J L

I am a newbie, so this comment may be way off...

Would a hashtable work best? I dont understand reading XML yet but
seems you could use your tag names as the key and store the value. No
need to redim.

John
 
J

J L

Hi Jason...me the newbie again.

Check out this link to learn about using datasets for this type of
need:

http://www.codeproject.com/vb/net/ConfigOpt.asp

I use it to create a custom config file, as he suggests but it is
generic in that you can create the datatable with columns for Tag and
Value pairs. Then it will grow as you add items to the table.

John
 
J

Jason Knott

Hi Cor,

That helps a lot, thanks. I think the best way is to add a new table
each time the function is called, and name the table the same name as
the XML document passed into it. I may still need to use the
XMLNodeReader object to read each of the attributes, though. By the
way, do you know if there is a way in the XMLNodeReader to get the
attribute name, not just the value? For example, if I had something
like <directory Path="C:\MyDirectory\MySubDirectory"/> Could I read in
the attribute name of "Path"? This way, I would be able to name each of
the columns in the datatable, then populate the rows with the attribute
values.

Thanks again,

-Jason
 
J

Jason Knott

Hi John,

The problem with a HashTable is (I believe) the key values need to be
unique. This is not always the case with an XML document. Here's an
example of an XML document I could be reading in:

<configuration>
<directories>
<directory path="C:\myPath"/>
<directory path="C:\myPath\subdir"/>
<directory path="C:\mySecondPath"/>
<directory path="C:\myThirdPath"/>
</directories>
</configuration>

So if I used a hashtable, the key would always be "directory". I
thought about making the key with the attribute values, however I can't
guarantee that even they will be unique values.

-Jason
 

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

Top