Adding A Row To An XML File And Read Data From XML To A DataGrid

I would like to know how is possible to add a row to an xml file for example i have this data:

<?xml version="1.0" encoding="utf-8" ?>
<list xmlns="http://tempuri.org/list.xsd">
      <lips>
            <te></te>
            <na></na>
            <de></de>
            <st></st>
            <lo></lo>
            <pa></pa>
            <op></op>
      </lips>
</list>

and through my program i would like to add a new row like <lips> with textbox content then save it to the xml file, is that possible? I would like to have some code examples.

I also would like to know how can I read from an XML file and then place my data in a DataGrid.
LVL 3
B055Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

james-ct16Commented:
hi
the following is the solution i use

##############
when loading
###############

allConfigs = New DataSet("settings")
          path = Application.StartupPath & "\config.conf"
            If System.IO.File.Exists(path) Then
                allConfigs.ReadXml(path)
                'check for source table
                If Not allConfigs.Tables.Contains(srcTBLName) Then
                    'build the source table
                    allConfigs.Tables.Add(createSourceTable)
###############
sub
#####
function createsourceTable as datatable
 Dim newSources As New DataTable(srcTBLName)
            newSources.Columns.Add(New DataColumn("sourcedesc", Type.GetType("System.String")))
            newSources.Columns("sourcedesc").AllowDBNull = False
   
           #create as many rows as you need  

 return new sources
end function

############
#at this point you have a dataset with a datatable in
############

####
so we how need to get the data tack to the xml
#######

sub textboxsubmit() handles [submit button]

dim newRow as datarow
newrow=allconfigs.tables(sourcetable).newrow()
newrow("coloumn name")=textbox1.text
######
#now do this for each text box
######
allconfigs.tables(sourcetable).rows.add(newrow)

'now write out the xml
writeXML
end sub

#####
writexml sub
#####

sub writeXML()
allConfigs.WriteXml(path)
End sub

###########

thats about it

sorry its a little scrappy as theres a fair bit of repetitive code to be added there

when looking through the code rember a couple of the varaibles there particuly allConfigs (the dataset) is declared as a private varaible in the class, similer to path and a couple of the dattable names

hope its of help

james
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
B055Author Commented:
the saving part is ok 10x but think we have a misunderstanding the loading question was, how can i put the data from an XML file in a DataGrid (that grid in the VB.net toolbox) not in a dataset
0
GlomCommented:
Hi,

To print data from a XML file in to a DataGrid,  do this :

Dim DS as New DataSet

DS.ReadXML("YourXMLFile.xml")

YourDataGrid.DataSource = DS

If you are on a web project, add also :

YourDataGrid.DataBind()

And to add a row to your XML file, first load your file in your DataSet, then add a row to it, and then call the WriteWML Method.

Glom
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.