J
jez
Perhaps a rather easy question for most of you but I'm stuck on it.
1) I've been trying for the past couple of days to save my dataset to
an XML file. I used to be able to save it without problem by using the
following method :
CustomersDS.WriteXml("deliveredCustomers.xml",
XmlWriteMode.WriteSchema);
now I get the following error : "System.Data.DataSet.WriteXml(string,
System.Data.XmlWriteMode)' is inaccessible due to its protection
level" - what does that mean? My dataset is initialised as private, I
changed it to public but still get the same problem.
2) I've also been trying to save directly to an XML file without going
through a DataSet (should save time!) by using the streamwriter and
the innerXml method. I've created a rather simple form with a couple
of textboxes that I want to save to XML when I click on a button.
This is what the code looks like :
private void buildXmlDoc() {
XmlDocument xmlDoc = new XmlDocument();
XmlNode deliveredCustomersNode;
XmlNode nameNode;
XmlNode emailNode;
xmlDoc.Load("customersDelivered.xml");
deliveredCustomersNode = xmlDoc.CreateElement("delivered");
xmlDoc.DocumentElement.AppendChild(deliveredCustomersNode);
nameNode = xmlDoc.CreateElement("Name");
nameNode.InnerText = listBox1.SelectedValue.ToString();
deliveredCustomersNode.AppendChild(nameNode);
emailNode = xmlDoc.CreateElement("Email");
emailNode.InnerText = textBoxEmail.Text;
deliveredCustomersNode.AppendChild(emailNode);
//create a FileStream obj so we can save the XML file
FileStream strmSaveToFile = new FileStream("customersDelivered.xml",
FileMode.OpenOrCreate,FileAccess.Write);
//instantiating a DataWriter obj so we can write the XML file
to disk
StreamWriter wrDataWriter = new StreamWriter(strmSaveToFile);
wrDataWriter.Write(xmlDoc.InnerXml);
wrDataWriter.Close();
MessageBox.Show("XML file saved!");
textBoxAddress.DataBindings.Clear();
textBoxPhone.DataBindings.Clear();
textBoxEmail.DataBindings.Clear();
textBoxCountry.DataBindings.Clear();
textBoxAddress.Text="";
textBoxPhone.Text="";
textBoxEmail.Text="";
textBoxCountry.Text="";
listBox1.SelectedIndex=0;
DataSet _CustomersDS = new DataSet("Customers");
_CustomersDS.ReadXml("customersDelivered.xml");
bindControls();
}
This method however doesn't seem to work.
Does the deliveredCustomers.xml file need to exist before I can use
this method ?
Is there any way I can add/append data to the end of the xml file ?
Thanks a lot !
jez
1) I've been trying for the past couple of days to save my dataset to
an XML file. I used to be able to save it without problem by using the
following method :
CustomersDS.WriteXml("deliveredCustomers.xml",
XmlWriteMode.WriteSchema);
now I get the following error : "System.Data.DataSet.WriteXml(string,
System.Data.XmlWriteMode)' is inaccessible due to its protection
level" - what does that mean? My dataset is initialised as private, I
changed it to public but still get the same problem.
2) I've also been trying to save directly to an XML file without going
through a DataSet (should save time!) by using the streamwriter and
the innerXml method. I've created a rather simple form with a couple
of textboxes that I want to save to XML when I click on a button.
This is what the code looks like :
private void buildXmlDoc() {
XmlDocument xmlDoc = new XmlDocument();
XmlNode deliveredCustomersNode;
XmlNode nameNode;
XmlNode emailNode;
xmlDoc.Load("customersDelivered.xml");
deliveredCustomersNode = xmlDoc.CreateElement("delivered");
xmlDoc.DocumentElement.AppendChild(deliveredCustomersNode);
nameNode = xmlDoc.CreateElement("Name");
nameNode.InnerText = listBox1.SelectedValue.ToString();
deliveredCustomersNode.AppendChild(nameNode);
emailNode = xmlDoc.CreateElement("Email");
emailNode.InnerText = textBoxEmail.Text;
deliveredCustomersNode.AppendChild(emailNode);
//create a FileStream obj so we can save the XML file
FileStream strmSaveToFile = new FileStream("customersDelivered.xml",
FileMode.OpenOrCreate,FileAccess.Write);
//instantiating a DataWriter obj so we can write the XML file
to disk
StreamWriter wrDataWriter = new StreamWriter(strmSaveToFile);
wrDataWriter.Write(xmlDoc.InnerXml);
wrDataWriter.Close();
MessageBox.Show("XML file saved!");
textBoxAddress.DataBindings.Clear();
textBoxPhone.DataBindings.Clear();
textBoxEmail.DataBindings.Clear();
textBoxCountry.DataBindings.Clear();
textBoxAddress.Text="";
textBoxPhone.Text="";
textBoxEmail.Text="";
textBoxCountry.Text="";
listBox1.SelectedIndex=0;
DataSet _CustomersDS = new DataSet("Customers");
_CustomersDS.ReadXml("customersDelivered.xml");
bindControls();
}
This method however doesn't seem to work.
Does the deliveredCustomers.xml file need to exist before I can use
this method ?
Is there any way I can add/append data to the end of the xml file ?
Thanks a lot !
jez