I pretty sure I have to create some sort of mold of what the XML file has to look like first, right?
Any help will be appreciated.
I pretty sure I have to create some sort of mold of what the XML file has to look like first, right?
Any help will be appreciated.
look at using Linq to xml - http://msdn.microsoft.com/en-us/library/bb387098.aspx there are tutorials here that will guide you through creating and querying an xml document.
One simple way to do this would be to create .NET classes that you put the data in and then use XmlSerializer to serialize the data to a file and then later deserialize back into an instance of the class and re-populate the form.
AS an example, if you have a form with customer data. To keep it short we will just have first name and last name. You can create a class to hold the data. Keep in mind this is just a simple example, you can store arrays and all kinds of complex/nested data like this.
public class CustomerData
{
public string FirstName;
public string LastName;
}
So save the data as XML your code will look something like this.
// Create an instance of the CustomerData class and populate
// it with the data from the form.
CustomerData customer = new CustomerData();
customer.FirstName = txtFirstName.Text;
customer.LastName = txtLastName.Text;
// Create and XmlSerializer to serialize the data to a file
XmlSerializer xs = new XmlSerializer(typeof(CustomerData));
using (FileStream fs = new FileStream("Data.xml", FileMode.Create))
{
xs.Serialize(fs, customer);
}
And loading the data back would be something like the following
CustomerData customer;
XmlSerializer xs = new XmlSerializer(typeof(CustomerData));
using (FileStream fs = new FileStream("Data.xml", FileMode.Open))
{
// This will read the XML from the file and create the new instance
// of CustomerData
customer = xs.Deserialize(fs) as CustomerData;
}
// If the customer data was successfully deserialized we can transfer
// the data from the instance to the form.
if (customer != null)
{
txtFirstName.Text = customer.FirstName;
txtLastName.Text = customer.LastName;
}
So, are you wanting to collect data from a user in a Windows form application and then write it out to an XML file (when they click OK)? If so, I'd check out the XmlTextWriter class ( http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.aspx ).