0

I have a program which is made for editing .xml files. I can edit and save .xml files, writing into the file is working properly, but when I edit and save another XML Table, it is overwriting the previous one.

My code:

            DataTable ds = (DataTable)dataGridView1.DataSource;
            ds.WriteXml(openFileDialog1.FileName);
            saved = true;
            MessageBox.Show("Successfully saved!", "Saving.");

I know why it is happening, the program is saving what is in the dataGridView at the moment of saving.

I have tried: ds.Merge(), TextWriters various type of Streams and FileModes

So how can I save the xml file without overwriting it completely?

Example:

This is the file what I have:

    <catalog>
       <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>
<catalog>

But when I edit the file, using another XML Table it gets overwritten, and it will look like this:

<catalog>
  <plant>
    <name>tulip</name>
  </plant>
</catalog>

Thanks for any kind of help!

Sorry for bad language.

MTn
  • 93
  • 7
  • A save is an overwrite. If you want to alter the file, you first have to read it, then alter it then save the whole file. You can't save part of a file, that makes no sense – Liam Aug 19 '21 at 08:11
  • Does this answer your question? [Modify XML existing content in C#](https://stackoverflow.com/questions/2551307/modify-xml-existing-content-in-c-sharp) – Liam Aug 19 '21 at 08:11
  • *You can't save part of a file* - well, you *can*.. but not with WriteXml – Caius Jard Aug 19 '21 at 08:14

1 Answers1

2

So how can I save the xml file without overwriting it completely?

That's what WriteXml does. If you want to load an XML file with one book in it (into a dataset with a books table?) and save it with one book and one plant in it then the dataset you save will have to represent the merged changes (it will have to have a books table and a plants table)

The thing I find confusing about your question is that "the program is saving what is in the datagridview" - a datagridview doesn't show a dataset, which is a collection of datatables - it shows a single datatable. Your DGV cannot simultaneously show a book and a plant. You should thus be absolutely sure the dataset contains a book and a plant even though you can only see one of them

Let's take your book XML there, fix the end tag, and load it:

enter image description here

At this time there is only the book table

Step on another 3 rows of code to where i've added another table, column and row value and there is another datatable in the set:

enter image description here

Choosing it in the visualizer changes the DGV to show the tulip:

enter image description here

And now WriteXml will write a merged file:

x

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Well: yes, I would like to have a. XML file with eg. a 'plant', and a 'book' table in it, but I can't find a way to merge the two tables. – MTn Aug 19 '21 at 08:17
  • 1
    You don't merge the tables; a book is not a plant. Your dataset has needs multiple tables, one for book and one for plant. See my edit. Note; if you're reading plants out of one xml file and books out of another, you need to transplant (hah) a table from one dataset to another (e.g. `bookDS.Tables.Add(some_datatable)`) but if the datatable already is inside a different dataset it must be removed first; a table can only be in one ds at a time – Caius Jard Aug 19 '21 at 08:25
  • Yes, I needed another Table, that is what I wanted, your code looks like exactly what I wanted, I will try it when I will have the time! Thanks, much appriciated! – MTn Aug 19 '21 at 08:29
  • Could you, please show me the code of how did you parse the book XML? From the pictures, I don't really understand it. – MTn Aug 19 '21 at 09:50
  • 1
    It's just that line `ds.ReadXml("c:\temp\a.xml")` - i took the xml out of your question and saved it in file in that path - the `catalog` becomes the dataset, the `book` becomes the table, the nodes within `book` become the columns names/values/types. It's not C# but that's jsut because I had a VB project open at the time - they're the same language, just remember that VB uses `()` for array access and c# uses `[]` so e.g. `ds.Tables["plant"].Rows.Add("tulip")...`. In case you never noticed, in modern VS methods are in yellow and properties are in white.. – Caius Jard Aug 19 '21 at 11:35