Question Dataset -> Add datarow using XmlElement?

robtyketto

Member
Joined
Jul 23, 2009
Messages
23
Programming Experience
Beginner
Greetings,

The main form in my application contains a datagridview which is bound to a datatable created from an xml document.

Another form is used to allow the adding of new rows, for this application the rows are quotes.

The code below creates an xmlElement using values from form on-screen control and appends to the xml document.

There are no problems here.

Now to refresh the datagridview I currently reload the WHOLE of the xml document and reset the datasource.

Using the xmlElement.outerxml would be a complete datarow/quote but no methods from the datatable amd xmlreaders/writers seem to accept xmlelement as a parameter.

Is it possible just to use the xmlElement contents and add it to the datagridview via adding it to the underlying datatable as a datarow rather than reloading the whole file again?

Hope this makes sense.

Code to create new xmlelement/datarow/quote
VB.NET:
            Dim quotedoc As New XmlDocument

            'Load xml data into the instance of xmldocument created above
            quotedoc.Load(cQuoteFileName)

            'Create the root node element i.e. quotefile for a NEW quote
            Dim xmlQuoteEl As XmlElement = quotedoc.CreateElement("quote")

            'Create nodes under the root element 
            xmlQuoteEl.InnerXml = "<date></date><author></author><category></category><text></text>"

            'Set details for quote
            xmlQuoteEl.Item("date").InnerText = dtpQuoteDate.Value.ToString("dd MMMM yyyy")
            xmlQuoteEl.Item("author").InnerText = txtAuthor.Text
            xmlQuoteEl.Item("category").InnerText = cboCategory.SelectedItem.ToString
            xmlQuoteEl.Item("text").InnerText = txtQuote.Text

            'append the whole element to the whole document.
            quotedoc.DocumentElement.AppendChild(xmlQuoteEl)

            'save the document now!
            quotedoc.Save(cQuoteFileName)

Code to load the xml file into the datagridview
VB.NET:
'Dim textReader As XmlTextReader = New XmlTextReader(
        Dim xmlquotesfile As New XmlDataDocument()
        xmlquotesfile.DataSet.ReadXml(cQuoteFileName)

        Dim xmlquotesdataset As New DataSet("quotesDataset")
        xmlquotesdataset = xmlquotesfile.DataSet
        xmlquotesdataset.Tables("quote").DefaultView.Sort = "category ASC"

        'Bind the dataset -> datatable to the datagridview 
        frmMainQuote.dgvQuotes.DataSource = xmlquotesdataset.Tables("quote").DefaultView

Thanks
Rob
 
Back
Top