Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.net] Datagridview <--> XML (read and write)

Hi at all!

I have a problem with writing form a datagridview into an XML file. Before that I read data from an XML a show the data in an DataGridView. This works correctly. If I add now some Data to the Datagridview and try to write the new data into the existing XML file. But unfortunately the Data is written into the wrong node.

I read the data form the XML to the datagridview like that:
Code
DataSet As New DataSet
        'reads the xml-File
        DataSet.ReadXml(xmlPath)
        'write the data in the datagridView
        Me.DataGridView1.DataSource = DataSet
        Me.DataGridView1.DataMember = "book"

I try to write data like that:
Code
DataSet.WriteXml(newXmlPath)

My XML file looks like that:
Code
<?xml version="1.0"?>
<catalog>
    <book_list>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications
          with XML.</description>
       </book>
       <book id="bk102">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies,
          an evil sorceress, and her own childhood to become queen
          of the world.</description>
       </book>
   </book_list>
</catalog

But if I add now a new row to the datagridview and try to save the new datagridview in the XML the XML file is saved like this:
Code
<?xml version="1.0"?>
<catalog>
    <book_list>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications
          with XML.</description>
       </book>
       <book id="bk102">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies,
          an evil sorceress, and her own childhood to become queen
          of the world.</description>
       </book>
   </book_list>
   <book>
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are
      integrated into a comprehensive development
      environment.</description>
   </book>
</catalog>

So my problem is now that the data will be added to another node not to the node I want to have the data. Which other possibility I have to write into the XML file keeping the old node strucuture?
Dim ds As New DataSet
ds.ReadXml(xmlFile)
DataGridView1.DataSource = ds.Tables(0)
(Dec 4, 2013 10:49 AM)Only3 Wrote:  Dim ds As New DataSet
ds.ReadXml(xmlFile)
DataGridView1.DataSource = ds.Tables(0)

Could you please be a little bit more precisely? I don't know what mean with these three lines of code
oo u want to write it with the changes
ur problem is with writing to the xml file > adding node
http://stackoverflow.com/questions/15616...sual-basic
then u can load it again


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Nemo2478Jul 25, 2020 12:01 PM 1 177 Jul 25, 2020 12:01 PM
Last Post: glitchgxd
[C] by zNinjaApr 5, 2020 01:24 PM 3 144 Apr 5, 2020 01:24 PM
Last Post: Mysticism
[.NET] by Takehito MorokuzuJun 9, 2019 03:51 PM 2 219 Jun 9, 2019 03:51 PM
Last Post: Hawk HF