Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

seralization and deseralization frpm xml with c#

$
0
0

Hello
I'm going to make a code to serialize and deserialize XML files to and from datagirdview.
for the seralization:
I have 2 XML files one called
person. xml
<Person>
<ID> 1 </ ID>
<name> jack </ name>
<Age> 28 </ age>
</ Person>
<Person>
<ID> 2 </ ID>
<name> jacline </ name>
<Age> 22 </ age>
</ Person>
<Person>
<ID> 3 </ ID>
<name> theo </ name>
<Age> 25 </ age>
</ Person>
......

empeloyeur.xml

<Empeloyeur>
<ID> 1 </ ID>
<Job> engineer </ job>
</ Empeloyeur>
<Empeloyeur>
<ID> 2 </ ID>
<Job> Director </ job>
</ Empeloyeur>
.........

I have this code to display the person.xml file contents in a datagirdview:

  private void fileOpenToolStripMenuItem_Click (object sender, EventArgs e)
         {
           
             opf.Filter = "Text documents (.xml) | * .xml";
           
             if (opf.ShowDialog () == DialogResult.OK)
             {
                 string path = opf.FileName;
                  DataSet dataSet = new DataSet ();
                 DataSet.ReadXML (path);
                 dataGridView1.DataSource = dataSet.Tables [0];

        
             }
         }

but I would like to show that the attribut <name> of file persone xml file  and the attribue <job> of  empolyeur.xml and in the same datagirdview ( datagirdview1 )

is that possible ? 

for the deserialize xml i have this code 

private void button1_Click(object sender, EventArgs e)
        {
        
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.TableName = "person";
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            ds.Tables.Add(dt);
   
            DataRow row = ds.Tables["person"].NewRow();
            row["ID"] = textBox1.Text;
            row["Name"] = textBox2.Text;
            row["Age"] = textBox3.Text;  
            ds.Tables["person"].Rows.Add(row);
            ds.WriteXml(path);
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        
        }

but this code gives me in the end only the last attributes. i went  a file that contains several celull <person> </ person> not only the last one I entered 

any help please ? 


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>