ADO.NET: using xml as a database, adding rows to the xmlfile

K

Kai Thorsrud

Hi,

I've got a debug/learn function for doing my xml thingie.
I simply want to append data to an xml file. This confuses me, i've looked
at xmldocument, xmldatadocument and from what i understand a DataSet is a
flexible and "easy" (if you know how) way to work with xml files. I've
worked alot with ADO in VB6 so this seemed tempting (i'm still a .net noob
or NOOB)

I've managed to create a schema for my table and i think i know how to add
the data correctly but i
can't manage to update my data back to my DataSet and write the DataSet as
an XMLFile. I've looked on the DataAdapter but i end up wondering how do i
make an OleDB conn to my xml file and i resemble nooo this seems stupid and
bigfoot print'ed since they say .net and xml is like bread and butter. I
just want to use an xml file as a simple database for my app.

Would anyone be so kind to help me out with a updatetable thingie ?

My Code fails on the AcceptChanges which i suppose is because my DataRow
isn't written back to the DataSet but how do i do that ?

Please ignore the bad naming convention this code is mixed with a lot copy
paste from various examples... none covers how to work with just a dataset
and an xml file.. I think i fail to see the wood because of the forest if
you know what i mean :)




Public Function doCreateNewXmlfile()
Dim ds As New DataSet
Dim col As DataColumn
Dim oDataRow As DataRow

'Create the table structure.
With ds.Tables.Add("Routers")
col = .Columns.Add("IPAddr", GetType(String))
col.MaxLength = 15
col = .Columns.Add("HostName", GetType(String))
col.MaxLength = 90
col = .Columns.Add("LogFileName", GetType(String))
col.MaxLength = 30
End With
' Save our schema representing our table structure
Dim oFileStream As New
System.IO.FileStream(Me.setXmlIpTableFileSchemaName, FileMode.Create)
ds.WriteXmlSchema(oFileStream)
oFileStream.Close()
oFileStream = Nothing

' Create some data in our xmldatabase
Dim row As DataRow = ds.Tables("Routers").NewRow
row("IPAddr") = "192.168.1.1"
row("HostName") = "tower"
row("LogFileName") = "tower_Log"

row.AcceptChanges()
oFileStream = New System.IO.FileStream(Me.setXmlIpTableFileName,
FileMode.Create)
ds.WriteXml(oFileStream)
oFileStream.Close()
oFileStream = Nothing
End Function

Thanks a lot
/Kai
 
M

Mohamoss

Hi kia

Since you are just using an xml file as your database , you don’t really
need an OleDB conn or a DataAdapter . you need these object if your are
connectinf to a DBMS .

However since you are only using xml files you don’t need all this , you
don’t even need to be using System.Data.OleDB namespace J .

You need only System.Data namespace .

1- Create a dataset Object

2- Create all the tables that you will be using with rows and columns
( I think you already know how to do that ) .

3- ADD the tables to the table collection of your dataset . so if your
dataset is DS , use DS.Tables.Add( thenameof the table );

4- After you are done with your work you write all the dataset to the
xml file you are using . this is done by the method DS.WtiteXml( “ the xml
file to wrie to “ , System.Data.XmlWriteMode.DiffGram) // this second pram
is the write mode Diffgram means that all the changes to the data will be
written (old and new values)

5- Next time you start your program add this line to the load event of
the first form of your program

DS.ReadXml(“the source file ”, the mode);

This will load all the data to the data set , then you can read , edit ,
modify in the dataset and rewrite back to the xml file’ as in step four ,
before you close your program.

I hope this would help,
 
K

Kai Thorsrud

Hi!

Thanks for the reply but i was a bit unclear in my previous post. What i
cannot manage to do is to put the actual data into my table/rowstructure.

I've tried adding the data directly into the dataset with the
oDsDataSet.Tables("Routers").Rows.Add but that seems to add new rows to the
table not a new line of data into my already defined table structure. I've
also tried to use the DataRow like this

' Create some data in our xmldatabase
oDataRow = oDsDataSet.Tables("Routers").NewRow
oDataRow("IPAddr") = "192.168.1.1"
oDataRow("HostName") = "tower"
oDataRow("LogFileName") = "tower_Log"
oDataRow.EndEdit()
Console.Write(oDataRow.RowState)
oDataRow.AcceptChanges()
oDsDataSet.WriteXml(Me.setXmlIpTableFileName)

but the data is not written back to the DataSet since no changes appear when
i do a AcceptChanges i get a : "Cannot perform this operation on a row not
in the table." or when i try to skip the AcceptChanges and do a .writexml no
data is changed....

As i said i can't see the "wood because of the forest" as we say here in
norway... please could you give me a push in the right direction ?

Thanks a lot

B Regards
Kai
 
K

Kai Thorsrud

Adding one more thing: the dataset holds no data but i've created a schema
with the table structure as i mentioned in my first post like this:

Public Function doCreateNewXmlfile()
Dim oDsDataSet As New DataSet
Dim col As DataColumn
Dim oDataRow As DataRow
Dim oXmlDataDoc As New System.Xml.XmlDataDocument


'Create the Schema for the dataset

oDsDataSet.Namespace = "Devices"

With oDsDataSet.Tables.Add("Routers")
col = .Columns.Add("IPAddr", GetType(String))
col.MaxLength = 15
col = .Columns.Add("HostName", GetType(String))
col.MaxLength = 90
col = .Columns.Add("LogFileName", GetType(String))
col.MaxLength = 30
End With

' Save our schema representing our table structure

oDsDataSet.WriteXmlSchema(Me.setXmlIpTableFileSchemaName)

' Create some data in our xmldatabase

oDataRow = oDsDataSet.Tables("Routers").NewRow
oDataRow("IPAddr") = "192.168.1.1"
oDataRow("HostName") = "tower"
oDataRow("LogFileName") = "tower_Log"
oDataRow.EndEdit()

Console.Write(oDataRow.RowState)

oDataRow.AcceptChanges()

oDsDataSet.WriteXml(Me.setXmlIpTableFileName)
 
K

Kai Thorsrud

I can see the forest now and it's beautiful (small children, great
pleasures)

damn me... i've used both the DataSet.Table(0).Rows and a DataRow... but
i've been munging around soo long that i failed to see that i had to pass
the DataRow to the .Rows.Add(DataRow)..

It works
haha
 
M

Mohamoss

Hi Kai
I am glad that you got the solution to your problem . i didn;t realize that
you didn't know how to add rows from the outset to the dataset .

To add to your joy , you don’t have to create row objects then add them to
the dataset you can just add data to the dataset while creating the row



myrow["col1"]= 11.1;

mytable.Rows.Add(myrow);

mytable.Rows.Add(new object[]{12.2});

mytable.Rows.Add(new object[]{14.7});

and here is an example , that do some deletion as well , that I was working
with .







myset = new DataSet();

mytable = new DataTable("trial");

mycolumn = new DataColumn("col1" ,
Type.GetType("System.Decimal"));

mytable.Columns.Add(mycolumn);

DataRow myrow = mytable.NewRow();

myrow["col1"]= 11.1;

mytable.Rows.Add(myrow);

mytable.Rows.Add(new object[]{12.2});

mytable.Rows.Add(new object[]{14.7});

myset.Tables.Add(mytable);

DataRowCollection r = mytable.Rows;

for( int idx = 0; idx < r.Count; idx++ )

{

DataRow row = mytable.Rows[ idx ];

if( row.RowState == DataRowState.Deleted )

{

if( row[ mycolumn,
DataRowVersion.Original ].ToString()== "12.2" )

{

r.RemoveAt( idx );

idx--;

}

}

else

{

if( row[ mycolumn].ToString() == "12.2" )

{

r.RemoveAt( idx );

idx--;

}

}

}



myset.WriteXml("c:\\temp.xml ",
System.Data.XmlWriteMode.DiffGram);

}


--
Mohamed Mossad
Microsoft GTSC Developer support for Middle East




news:[email protected]...
 

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