LINQ to XML and DataGridView

2019-04-02 02:48发布

问题:

Hi I’m trying to use a DataGridView for the first time and with LINQ.

Here’s what I’m trying to do: I want to use it (although it doesn’t have to the DataGridView ) to read and display the contents of an XML file (this bit is working code below) but I want to have an add row button on the form or in the DataGridView that takes the contents of three textboxes and and populates the contents of three columns of the new row. The new row is to be appended to the existing data.

Next I would like to add a Delete button to delete the currently selected row.

And lastly I want a save button that exports the contents of the DataGridView back to an XML file overwriting/updating the existing XML file.

So I stuck at adding the additional data! but as I don't currently have a clue as to the delete or save either I thought I would ask all in one go!!

So this is the code I have to read the xml file:

XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
        select new
        {
            QueueNumber = c.Element("Number").Value,
            QueueName = c.Element("Name").Value,
            QueuePCC = c.Element("QueueTag").Value
        };

dataGridView1.DataSource = q.ToList();

XML document:

<?xml version="1.0" encoding="utf-8" ?>
<Queues>
  <Queue>
    <Number>
      001
    </Number>
    <Name>
      mytest
    </Name>
    <QueueTag>
      xyz
    </QueueTag>
  </Queue>
  <Queue>
    <Number>
      002
    </Number>
    <Name>
      Adi2
    </Name>
    <QueueTag>
      ABCD
    </QueueTag>
  </Queue>
</Queues>

ok I have now changed my code to this:

XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
        select new Queue
        {
            Number = c.Element("Number").Value,
            Name = c.Element("Name").Value,
            QueueTag= c.Element("QueueTag").Value
        };

var queryAsList = new BindingList<Queue>(q.ToList());

bindingSource1.DataSource = queryAsList;
dataGridView1.DataSource = bindingSource1;

This allows me to add and delete rows and data from the dataGridView :)

But I can still find no way to writing the data back to XML either from the dataGridView or from the bindingSource1 :(

Any help please? the bindingSource1.count changes each time I make a change to the data so I think I'm close!

回答1:

Perhaps you may try working with the xmlDoc saved in the session (thus preserved with the postbacks)... Add the three elements (from the textboxes) to the XmlDoc... And rebind the gridview to the xmlDoc at each postback.

At the end, you could do xmlDoc.save().

For deleting rows, you could use the GridView event, dataGridView1_RowDeleted, to edit the xmlDoc (to remove the node relative to the selected row).

Something like:

protected void ButtonLoadGridView_Click(object sender, EventArgs e)
{
    XDocument xmlDoc = XDocument.Load(@"f:\queues.xml");
    var q = from c in xmlDoc.Root.Descendants("Queue")
            select new
            {
                QueueNumber = c.Element("Number").Value,
                QueueName = c.Element("Name").Value,
                QueuePCC = c.Element("QueueTag").Value
            };
    dataGridView1.DataSource = q.ToList();
    Session.Add("xmlDoc",xmlDoc);        
}

public void dataGridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
    // get some node information:

    int rowIndex = e.RowIndex;            
    string someNodeInfo = dataGridView1.Rows[rowIndex].Cells[0].Text;            

    // then edit xml :
    XmlDocument xmlDoc = (XmlDocument) Session["xmlDoc"];
    xmlDoc.ChildNodes.Item(rowIndex).RemoveAll();
    Session.Add("xmlDoc", xmlDoc);
}


protected void ButtonSave_Click(object sender, EventArgs e)
{
    XmlDocument xmlDoc = (XmlDocument)Session["xmlDoc"];
    xmlDoc.Save(@"f:\queues2.xml");
}