I pretty sure I have to create some sort of mold of what the XML file has to look like first, right?
我非常肯定我必须首先创建一些XML文件的模型,对吧?
Any help will be appreciated.
任何帮助将不胜感激。
3 个解决方案
#1
12
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.
一种简单的方法是创建将数据放入的.NET类,然后使用XmlSerializer将数据序列化为文件,然后反序列化为类的实例并重新填充表单。
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.
因此,将数据保存为XML,您的代码将如下所示。
// 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;
}
#2
1
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.
看看使用Linq到xml - http://msdn.microsoft.com/en-us/library/bb387098.aspx这里有一些教程,将指导您创建和查询xml文档。
#3
0
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 ).
那么,您是否希望从Windows表单应用程序中的用户收集数据,然后将其写入XML文件(当他们单击确定时)?如果是这样,我将查看XmlTextWriter类(http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.aspx)。
#1
12
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.
一种简单的方法是创建将数据放入的.NET类,然后使用XmlSerializer将数据序列化为文件,然后反序列化为类的实例并重新填充表单。
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.
因此,将数据保存为XML,您的代码将如下所示。
// 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;
}
#2
1
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.
看看使用Linq到xml - http://msdn.microsoft.com/en-us/library/bb387098.aspx这里有一些教程,将指导您创建和查询xml文档。
#3
0
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 ).
那么,您是否希望从Windows表单应用程序中的用户收集数据,然后将其写入XML文件(当他们单击确定时)?如果是这样,我将查看XmlTextWriter类(http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.aspx)。