黑马程序员技术交流社区

标题: c#使用linq技术创建xml文件的小例子 [打印本页]

作者: 陈君    时间: 2014-9-5 22:30
标题: c#使用linq技术创建xml文件的小例子
c#使用linq技术创建xml文件的小例子,需要的朋友可以参考一下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Xml;
  11. using System.Xml.Linq;
  12. namespace CreateXMLByLINQ
  13. {
  14. public partial class Frm_Main : Form
  15. {
  16. public Frm_Main()
  17. {
  18. InitializeComponent();
  19. }
  20. static string strPath = "Employee.xml";
  21. //创建XML文件
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. XDocument doc = new XDocument(//创建XML文档对象
  25. new XDeclaration("1.0", "utf-8", "yes"),//添加XML文件声明
  26. new XElement(textBox1.Text,//创建XML元素
  27. new XElement(textBox2.Text, new XAttribute(textBox3.Text, textBox10.Text),//为XML元素添加属性
  28. new XElement(textBox4.Text, textBox5.Text),
  29. new XElement(textBox6.Text, textBox7.Text),
  30. new XElement(textBox8.Text, textBox9.Text))
  31. )
  32. );
  33. doc.Save(strPath);//保存XML文档
  34. MessageBox.Show("XML文件创建成功");
  35. }
  36. }
  37. }
  38. <DIV class=blockcode>
  39. <BLOCKQUOTE><?xml version="1.0" encoding="UTF-8" standalone="true"?>
  40. -<Peoples> -<People ID="001"> <Name>123</Name> <Sex>123</Sex> <Salary>123</Salary> </People> </Peoples>
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2