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>
......
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 of file persone xml file and the attribue 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 not only the last one I entered
any help please ?
Xml is case sensitive. Below I posted update xml files and code
Xml 1
<?xml version="1.0" encoding="utf-8" ?>
<root>
<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>
</root>
Xml 2
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Empeloyeur>
<ID>1</ID>
<Job>engineer </Job>
</Empeloyeur>
<Empeloyeur>
<ID>2</ID>
<Job>Director</Job>
</Empeloyeur>
</root>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME_1 = @"c:\temp\test1.xml";
const string FILENAME_2 = @"c:\temp\test2.xml";
static void Main(string[] args)
{
XDocument doc1 = XDocument.Load(FILENAME_1);
XDocument doc2 = XDocument.Load(FILENAME_2);
var results = (from d1 in doc1.Descendants("Person")
join d2 in doc2.Descendants("Empeloyeur") on (int)d1.Element("ID") equals (int)d2.Element("ID")
select new { person = d1, empelour = d2 })
.ToList();
DataTable dt = new DataTable("person");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("Job", typeof(string));
foreach(var result in results)
{
dt.Rows.Add(new object[] { (int)result.person.Element("ID"), (string)result.person.Element("name"), (int)result.person.Element("Age"), (string)result.empelour.Element("Job") });
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.