程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> c#使用linq技術創建xml文件的小例子

c#使用linq技術創建xml文件的小例子

編輯:C#基礎知識

代碼如下:

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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved