Fatest method for accessing xml

C

Chris

Alright,

Ive just began diving into the lovely world of .net cf dev. I have an
xml file similar to the one below:

<Root Database="accvalquery.xml">
<Structure>
<Field>
<Field_Name>QNAME</Field_Name>
<Field_Type>Character</Field_Type>
<Field_Len>10</Field_Len>
<Field_Dec/>
</Field>
<Field>
<Field_Name>QTXTVAL</Field_Name>
<Field_Type>Character</Field_Type>
<Field_Len>49</Field_Len>
<Field_Dec/>
</Field>
<Field>
<Field_Name>QVAL</Field_Name>
<Field_Type>Character</Field_Type>
<Field_Len>4</Field_Len>
<Field_Dec/>
</Field>
</Structure>
<Information>
<Record>
<QNAME>rover</QNAME>
<QTXTVAL>metro</QTXTVAL>
<QVAL>1</QVAL>
</Record>
<Record>
<QNAME>rover</QNAME>
<QTXTVAL>214</QTXTVAL>
<QVAL>2</QVAL>
</Record>
<Record>
<QNAME>Citroen</QNAME>
<QTXTVAL>Zara</QTXTVAL>
<QVAL>1</QVAL>
</Record>
</Information>
</Root>

Below is the code i have written to access this file and perform a
filter to only get me certain records then add them to the combo box.
Currently it takes about 5secs although this hasnt been timed
properly. Before i continute to use this method on other files (100
records or so) would like some experts to tell me if this the fatest
(and most efficient) way?

Dim ds As New DataSet
Dim datar As DataRow


ds.ReadXml("/Program
Files/SmartDeviceApplication1/accvalquery.xml")
Dim foundRows As DataRow() = ds.Tables(3).Select("QNAME =
'rover'")

Dim r As DataRow
Dim c As DataColumn

For Each r In foundRows
ComboBox1.Items.Add(CType(r(1), String)) ' add the model
Next r
Thanks

Chris
 
R

Russ Gray

Chris said:
Dim ds As New DataSet
Dim datar As DataRow


ds.ReadXml("/Program
Files/SmartDeviceApplication1/accvalquery.xml")
Dim foundRows As DataRow() = ds.Tables(3).Select("QNAME =
'rover'")

Dim r As DataRow
Dim c As DataColumn

For Each r In foundRows
ComboBox1.Items.Add(CType(r(1), String)) ' add the model
Next r

That's one of the easiest ways, but not the fastest. Have a look at the
XmlTextReader class, which involves more work but should give you a
noticeable speed boost, especially if you trust the source of the xml
file and therefore don't need to do any validation.
 
C

Chris

Russ Gray said:
That's one of the easiest ways, but not the fastest. Have a look at the
XmlTextReader class, which involves more work but should give you a
noticeable speed boost, especially if you trust the source of the xml
file and therefore don't need to do any validation.

Could you possibly provide a code sample reading a dataset using XmlTextReader?
 
R

Russ Gray

Chris said:
Could you possibly provide a code sample reading a dataset using XmlTextReader?

//-----------------------------------------------------------

string s;
XmlTextReader xr = new XmlTextReader("/Program
Files/SmartDeviceApplication1/accvalquery.xml");

xr.MoveToContent();
while (xr.Read())
{
if (xr.NodeType.Equals(XmlNodeType.Element)
&& xr.Name.Equals("QNAME"))
{
s = xr.ReadElementString();
if (s == "rover")
ComboBox1.Items.Add(s);
}
}
xr.Close();

//-----------------------------------------------------------

Obviously this isn't very flexible because I've used your example
hardcoded values. For more flexibility, you might want to use a switch
statement to test for different values of xr.Name. Note that the speed
benefit of an XmlTextReader is likely to be negligible on small xml
files, so do some testing and figure out if it's worth paying a speed
penalty for the ease of use of a DataSet.

Also, in your sample XML document, you might want to look at writing a
schema to describe your data, rather than defining your own <Structure>
data, which is rather reinventing the wheel.

HTH.
 
G

Glyn Meek

Chris...just to add my 2 cents worth here. We use xml access extensively,
and while we have got it to work perfectly, it is still SLOW on .net CF, no
matter how much you try to optimize, so be aware that you might still not
get the performance you want/need.

Glyn Meek
 
Top