如何以XML格式存储数据? [英] How do I store the data in XML?
本文介绍了如何以XML格式存储数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在提交表单后有一个表单,值应存储在xml文件中..怎么做..请逐步解释..
问候
Raju
我尝试过:
liferay
i have a form after submitting the form the values should be stored in an xml file.. how to do it..pls explain step by step..
Regards
Raju
What I have tried:
New to liferay
推荐答案
XML简介序列化 [ ^ ]。
参考 XmlSerializer类(System.Xml.Serialization) [ ^ ]类
按照步骤
创建一个实体表单中的属性为
refer XmlSerializer Class (System.Xml.Serialization)[^] class
follow the steps
Create an entity with the properties in the form as
public class MyEntity
{
public string Name { get; set; }
public string Age { get; set; }
public string Address { get; set; }
}
以下代码用于使用Asp.net应用程序,但概念相同mvc或窗口等。
the below code is for using Asp.net application, however the concept is same for mvc or window etc..
protected void btnSave_Click(object sender, EventArgs e)
{
// create a folder 'xmlfiles' in the root directory
string filePath = Server.MapPath("xmlfiles") + "//" + "myfile.xml"; // Path for the xml
MyEntity entity = new MyEntity();
// initalise the properties of the entity
entity.Name = txtName.Text;
entity.Age = txtAge.Text;
entity.Address = txtAddress.Text;
// end
XmlSerializer xs = new XmlSerializer(typeof(MyEntity));
TextWriter tw = new StreamWriter(filePath);
xs.Serialize(tw, entity);
}
嗯......实现这个目标的方法很少:
1)正如前辈所写:XmlSerialization对你来说是最好的选择
XML序列化和反序列化:第1部分 [ ^ ]
XML序列化和反序列化:第2部分 [ ^ ]
自定义类集合序列化和反序列化的完整示例 [ ^ ]
2)使用 XDocument类(System.Xml.Linq) [ ^ ]
3) 使用 XmlDocument Class [< a href =https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]
4)使用 DataTable [ ^ ]的WriteXml方法(字符串) [ ^ ]和 ReadXml方法(字符串)(System.Data) [ ^ ]
试试!
Well... There's few ways to achieve that:
1) As the predecessors wrote: XmlSerialization is the best option for you
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]
2) using XDocument Class (System.Xml.Linq)[^]
3) using XmlDocument Class [^]
4) using DataTable[^]'s WriteXml Method (String)[^] and ReadXml Method (String) (System.Data)[^]
Try!
这篇关于如何以XML格式存储数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文