Manipulating XML data - using a DataSet

S

Scott M. Lyon

Part of an application I'm working on needs to manipulate some XML data (in
an XML file), by reading the data into a DataSet using the MyDataSet.ReadXML
method, and then using MyDataSet.WriteXML to update the XML file itself.


However, now I want to take advantage of the Primary Key functionality of
the DataSet object, so I can easier find rows (in a DataTable in the
DataSet), and also easier remove rows, based on a unique key.


Unfortunately, I'm not sure how that will impact how the XML looks (and I
cannot change the format of that file - it's already used by other
applications).


Here's a snippet of the XML I'm using:

<?xml version="1.0" ?>
<Cache>
<FileCache>
<FileName>Book1.xls</FileName>
<FilePath>C:\Windows\Temp</FilePath>
<OID>3838.64847.26094.60105</OID>
<DateTime>4/5/2005 9:50:45 AM</DateTime>
</FileCache>
<FileCache>
<FileName>Book2.xls</FileName>
<FilePath>C:\Windows\Temp</FilePath>
<OID>3838.64847.18503.25804</OID>
<DateTime>4/5/2005 9:50:56 AM</DateTime>
</FileCache>
</Cache>


The field I need to use as primary key (should be unique) is the OID field.

As you can see, reading that data into a Dataset gives me a DataTable called
Cache, with rows called FileCache (where OID is the third column in those
rows).


How do I tell it to use that column as the primary key, even after I've
loaded it via the LoadXML method, and then how do I do the SaveXML without
adding anything else to the XML layout than what you see above?


Thanks!
 
S

Scott M. Lyon

Never mind everybody... Managed to find the solution myself... :)

If anyone else is having this issue, here's my solution.

After the ReadXML, I added the following line:

MyDataSet.Tables("Cache").PrimaryKey = New DataColumn()
{_MyDataSet.Tables("Cache").Columns("OID")}
 

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