Hi guys,
i currently have this XML as data
<DEALS><DEAL ID="22" ISBN="0-7888-1623-3" Screenplay="Mary Kaplan" Title ="Mr Gentleman" Director = "Jonathan Jones"><ALLOCATION DEMANDE="5000" CODE="72" PRICE="25.00">2500</ALLOCATION><ALLOCATION DEMANDE="7000" CODE="75" PRICE="35.00">4000</ALLOCATION></DEAL><DEAL ID="23" ISBN="1-7988-1623-3" Screenplay="Joe Doe" Title ="Nothing Much" Director = "Listentome"><ALLOCATION DEMANDE="3300" CODE="72" PRICE="15.00">2500</ALLOCATION></DEAL></DEALS>
I load the data with the code below:
i use xDocument to load the DealData.xml and then put it in ToString.
//outside of the form
public static class XElementExtensions
{
public static DataTable ToDataTable(this XElement element)
{
DataSet ds = new DataSet();
string rawXml = element.ToString();
ds.ReadXml(new StringReader(rawXml));
return ds.Tables[0];
}
public static DataTable ToDataTable(this IEnumerable<XElement> elements)
{
return ToDataTable(new XElement("Root", elements));
}
}
//in the form
private void button2_Click(object sender, EventArgs e)
{
var xDocument = XDocument.Load("DealData.xml");
string txtxml = xDocument.ToString();
StringReader reader = new StringReader(txtxml);
XDocument doc1 = XDocument.Load(reader);
var res = doc1.Descendants("DEAL").ToList().Where(x => x.Attribute("ID").Value == "22").Descendants("ALLOCATION");
dataGridView1.DataSource = res.ToDataTable();
}Now my question is:
I would like to update the data from the DataGridview with the Attribute("ID").Value == "22" and save the data back to the XML.
For example, after I load my data on the datagridview, we only pickup anything that is in the node
<DEAL ID="22" ISBN="0-7888-1623-3" Screenplay="Mary Kaplan" Title ="Mr Gentleman" Director = "Jonathan Jones"><ALLOCATION DEMANDE="5000" CODE="72" PRICE="25.00">2500</ALLOCATION><ALLOCATION DEMANDE="7000" CODE="75" PRICE="35.00">4000</ALLOCATION></DEAL>
I updated the datagridview below with DEMANDE = 9000 CODE = 66 PRICE = 24.77 AND ALLOCATION = 1200
I want be able to extract the data back in the xml as a child of the DEAL ID = "22"
So that the XML file looks like this
<DEALS><DEAL ID="22" ISBN="0-7888-1623-3" Screenplay="Mary Kaplan" Title ="Mr Gentleman" Director = "Jonathan Jones"><ALLOCATION DEMANDE="5000" CODE="72" PRICE="25.00">2500</ALLOCATION><ALLOCATION DEMANDE="7000" CODE="75" PRICE="35.00">4000</ALLOCATION><ALLOCATION DEMANDE="9000" CODE="66" PRICE="24.77">1200</ALLOCATION> <-! this is the new line !-></DEAL><DEAL ID="23" ISBN="1-7988-1623-3" Screenplay="Joe Doe" Title ="Nothing Much" Director = "Listentome"><ALLOCATION DEMANDE="3300" CODE="72" PRICE="15.00">2500</ALLOCATION></DEAL></DEALS>
Is there a way to achieve that?
I have been searching and reading in the books but i cannot find a solution for this.
Thank you
Please do not forget to click “Vote as Helpful” if the reply helps/directs you toward your solution and or "Mark as Answer" if it solves your question. This will help to contribute to the forum.