c#使用linq技术创建xml文件的小例子
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.io;
using system.xml;
using system.xml.linq;
namespace createxmlbylinq
{
public partial class frm_main : form
{
public frm_main()
{
initializecomponent();
}
static string strpath = "employee.xml";
//创建xml文件
private void button1_click(object sender, eventargs e)
{
xdocument doc = new xdocument(//创建xml文档对象
new xdeclaration("1.0", "utf-8", "yes"),//添加xml文件声明
new xelement(textbox1.text,//创建xml元素
new xelement(textbox2.text, new xattribute(textbox3.text, textbox10.text),//为xml元素添加属性
new xelement(textbox4.text, textbox5.text),
new xelement(textbox6.text, textbox7.text),
new xelement(textbox8.text, textbox9.text))
)
);
doc.save(strpath);//保存xml文档
messagebox.show("xml文件创建成功");
}
}
}
<?xml version="1.0" encoding="utf-8" standalone="true"?>
-<peoples> -<people id="001"> <name>123</name> <sex>123</sex> <salary>123</salary> </people> </peoples>