• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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.
0
B055
Asked:
B055
1 Solution
 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now