Question add a row to datagridview by XML

Ron Miel

Member
Joined
Apr 9, 2011
Messages
16
Programming Experience
10+
I have a datasgridview. I can populate it from an XML file like this:

VB.NET:
  Public ds As New DataSet
   ...
   Dim xmlFile As XmlReader
   xmlFile = XmlReader.Create("products.xml", New XmlReaderSettings())
   ds.ReadXml(xmlFile)
   dg_products.DataSource = ds.Tables(0)

this is linked to the file, so that if I change the data manually, it wil be saved in the file.

Now, I perform a webrequest, and get a webresponse. It is one record in XML, the same format as the file.

I want to add the new record to the datagridview, and the linked file. What is the syntax for doing this?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
You can call ReadXml method multiple times to add new data to the existing DataTable.
 

Ron Miel

Member
Joined
Apr 9, 2011
Messages
16
Programming Experience
10+
Thank you, what's the syntax?

I've got the webresponse like this:

VB.NET:
  Dim response As WebResponse = request.GetResponse()
  Dim incoming_stream As Stream = response.GetResponseStream
  Dim sr As New StreamReader(incoming_stream)

   ' get the incoming stream into an XML document
   incoming_xml = New XmlDocument
   incoming_xml.LoadXml(sr.ReadToEnd())

how do I insert that into the datatable?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
what's the syntax?
Look here first: DataSet.ReadXml Method (System.Data)
Syntax is same as in your first post basically. You used XmlReader input to read data from a file, you could have just used the file path String.

The new data is coming from a Stream and it would be natural to use that as input for ReadXml when you want to add that new data to your existing DataTable.
DataTable also has the ReadXml method, that behaves the same and takes same kind of input: DataTable.ReadXml Method (System.Data)

Getting to your existing DataTable can be done either from DataSource where you put it (DirectCast from type Object to type DataTable), or you can keep a reference to it with a private class variable.
 

Ron Miel

Member
Joined
Apr 9, 2011
Messages
16
Programming Experience
10+
Thank you, I did in fact search the reference before asking. I still don't know hoe to do it. I can't get it to work.
I tried this, for example

VB.NET:
        Dim request As WebRequest = WebRequest.Create(req)
        Application.DoEvents()
        Dim response As WebResponse = request.GetResponse()
        Dim incoming_stream As Stream = response.GetResponseStream

        ds.ReadXml(incoming_stream)

It doesn't work. The new item does not show up on the datagridview. What am I doing wrong?

Also, I may want to edit the returned record before I add it to the table. For exampl;e it has a timestamp in an unfriendly format. I really don't need to show microseconds, I want it rounded to the nearest second. So it would be better if I can load a string or strings rather than an unaltered stream.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
It doesn't work. The new item does not show up on the datagridview. What am I doing wrong?
I did the exact same thing and it works for me. Is "ds" the DataSet object (or containing the DataTable) that you bound to DataSource?
Also, I may want to edit the returned record before I add it to the table. For exampl;e it has a timestamp in an unfriendly format. I really don't need to show microseconds, I want it rounded to the nearest second. So it would be better if I can load a string or strings rather than an unaltered stream.
Lots of options: Edit the field programmatically after is was added. Read the new data into its own DataTable, edit that, ImportRow into the other DataTable. Read the new data as Xml (Xdocument), manually add the modified data as a new row to DataTable.
 

Ron Miel

Member
Joined
Apr 9, 2011
Messages
16
Programming Experience
10+
I did the exact same thing and it works for me. Is "ds" the DataSet object (or containing the DataTable) that you bound to DataSource?
Yes, see the code in my first post.

Lots of options ... Read the new data as Xml (Xdocument), manually add the modified data as a new row to DataTable.

That's exactly what I want to do, but I can't work out the correct instruction to do that.
Please instruct me how to do that.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
Since you know how to load xml into a dataset, why not do that?
XDocument is not difficult though:
Dim doc = XDocument.Load(theStream)
Dim name = doc...<product>.<name>.Value
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
Are you kidding? No, seriously, are you?

That's what you did in first post with ReadXml.
 

Ron Miel

Member
Joined
Apr 9, 2011
Messages
16
Programming Experience
10+
No, I am not kidding. In the first post I loaded XML from a file. I can do that.

Now I want to load XML from an XmlDocument. I don't know how to do that.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
That not what you said, you said that you needed to load xml from a stream. I have already explained that you can do that with ReadXml method to read it into DataSet/DataTable.

It doesn't matter where the source of the xml comes from, or whether you read it into a DataTable or a XDocument.

XmlDocument by the way, is a completely different class than the XDocument class.
 

Ron Miel

Member
Joined
Apr 9, 2011
Messages
16
Programming Experience
10+
I will repeat my question. (see post#3)


I've got the webresponse like this:

VB.NET:
  Dim response As WebResponse = request.GetResponse()
  Dim incoming_stream As Stream = response.GetResponseStream
  Dim sr As New StreamReader(incoming_stream)

   ' get the incoming stream into an XML document
   incoming_xml = New XmlDocument
   incoming_xml.LoadXml(sr.ReadToEnd())

how do I insert that into the datatable?

This is my question. It has always been my question. I did not bring up the subject of streams. I do not want to know about streams.


And by the way, I tried ReadXml like this:

ds.ReadXml(incoming_xml.InnerXml)

It gives me an error saying 'illegal characters in path'. I presume it's looking for a filename, so it doesn't like the string I pass it.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
Correct syntax is:
ds.ReadXml(incoming_stream)

or see post 8 if you want to load it to a XDocument. XmlDocument is old and inconvenient.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
You can't, but you can create a new row with values and add it manually to the DataTable.
dt.Rows.Add("A", "B", "C")

These values can of course come from the XDocument node values, like the "name" value in example post 8.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
me said:
You can't
To elaborate on that. You can by using Xdocument.CreateReader that returns a XmlReader, which can be used as input for DataSet/DataTable.ReadXml, but that would have same effect as using ReadXml(inputStream) in the first place, which as you've explained can't work for you.
 
Top Bottom