tags:

views:

77

answers:

1

Hi,

I recently databounded an xml file to a listbox. Now I want to write some function to add or delete items from that listbox. The following error came up while trying listbox.items.add or listbox.items.insert:

Access and modify elements with ItemsControl.ItemsSource instead.

I googled this and it said to work on the 'model' instead,

However I have no clue how to do that. So how can I add or delete items to and from the datasource? I would hate having to add xml elements and values to the original and then update the file as the data source...

+1  A: 

How about loading the XML into a dataset and then manipulating the dataset and rebinding?

Instead of using the XML file and binding that to the listbox, try this: (source)

 string myXMLfile = "C:\\MySchema.xml";
    DataSet ds = new DataSet(); 
    try
    {
        //reads file and loads data into dataset
        ds.ReadXml(myXMLfile);
        //set the listbox's datasource to the dataset
        //note that if you have complex XML then you might need to set this to one of the datatables in the datset to get what you want
        listBox1.DataSource = ds;
        listBox1.DataBind();
    }
    catch (Exception ex)
    {
      //Handle error
    }

Lets assume you XML is rather simple. To add a tow to the DataSet you would do: (source)

//if customers is the name of your table (have not seen your XML)
DataRow newCustomersRow = ds.Tables["Customers"].NewRow();

newCustomersRow["CustomerID"] = "ALFKI";
newCustomersRow["CompanyName"] = "Alfreds Futterkiste";

dataSet1.Tables["Customers"].Rows.Add(newCustomersRow);

Here is an XML example that should work with the above:

<Data>
  <Customers>
    <Customer>
      <CustomerID>123</CustomerID>
      <CompanyName>Name</CompanyName>
    </Customer>
  </Customers>
</Data>

I have not compiled or tried to run any of that, but you should get the general idea and adapt it to your code.

kniemczak
Can you give me an example? What is the difference between a data source and a dataset?Currently I bind a file and then I set bindings for listitems so the box fills up..
internetmw
The data source is the source of the data you are binding to the control. In your case you are using the xml file as a datasource and bonding that data to the listbox. A datasource is a class that represents a table of data, which I imagine your XML is representing as well if you are binding to a listbox. I will update my post with an example.
kniemczak
And would that still work with two way databinding, so I can update the source file?
internetmw
Oh I misunderstood when you said you would hate to update the source. I thought you meant you did not want to have changes update the source XML. The default XML datasource does not allow updates tot he XML document by default. You would have to implement your own custom update to edit the XML document.
kniemczak
Allright, but now I have set up a two way binding and set the propertychanged event to save the original document. So for adding/deleting rows, can I update the datasource or do I have to edit directly in the source xml file?
internetmw
You can use the XML datasource, but you will need to implement your own code for the listbox update. Here is a solution for handling it with a gridview update. I am sure you can use it to fix your listbox.http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/12833c29-7a3b-4620-96bc-bfcd00ac2fc3
kniemczak
This seems to be something written in asp.net...I tried using it:{var provider = this.FindResource( "CUEData" ) as XmlDocument; XmlDocument myXml =provider.GetXmlDocument();but it says that system.xml.xmldocument doesn't know the getxmldocument...
internetmw