C#使用XmlDocument或XDocument創(chuàng)建xml文件
使用XmlDocument或XDocument創(chuàng)建xml文件,具體內(nèi)容如下
需引用:System.Xml; System.Xml.Linq;
1.使用XmlDocument創(chuàng)建xml(入門案例)
static void Main(string[] args) { //使用XmlDocument創(chuàng)建xml XmlDocument xmldoc = new XmlDocument(); XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); xmldoc.AppendChild(xmldec); //添加根節(jié)點(diǎn) XmlElement rootElement = xmldoc.CreateElement("school"); xmldoc.AppendChild(rootElement); //添加根節(jié)點(diǎn)下的子節(jié)點(diǎn)元素 XmlElement classElement = xmldoc.CreateElement("class"); rootElement.AppendChild(classElement); XmlAttribute atrrClass = xmldoc.CreateAttribute("No"); atrrClass.Value = "1"; classElement.Attributes.Append(atrrClass); //添加子節(jié)點(diǎn)下的元素 XmlElement stuElement = xmldoc.CreateElement("student"); classElement.AppendChild(stuElement); XmlAttribute attrStu = xmldoc.CreateAttribute("sid"); attrStu.Value = "20180101"; stuElement.Attributes.Append(attrStu); //保存文件 xmldoc.Save(@"d:\zzz\TestA.xml"); Console.WriteLine("創(chuàng)建xml文件ok!"); Console.ReadKey(); }
使用XmlDocument創(chuàng)建的xml文件:
2. 使用XDocument創(chuàng)建xml(入門案例)
static void Main(string[] args) { //使用XDocument創(chuàng)建xml System.Xml.Linq.XDocument xdoc = new XDocument(); XDeclaration xdec = new XDeclaration("1.0", "utf-8", "yes"); xdoc.Declaration = xdec; //添加根節(jié)點(diǎn) XElement rootEle = new XElement("school"); xdoc.Add(rootEle); //給根節(jié)點(diǎn)添加子節(jié)點(diǎn) XElement classEle = new XElement("class"); XAttribute attrClass = new XAttribute("No", 1); classEle.Add(attrClass); rootEle.Add(classEle); //添加子節(jié)點(diǎn)下的元素 XElement stuEle = new XElement("student"); XAttribute atrStu = new XAttribute("sid", "20180101"); stuEle.Add(atrStu); classEle.Add(stuEle); //保存文件 xdoc.Save("d:\\zzz\\TestB.xml"); Console.WriteLine("創(chuàng)建xml文件ok"); Console.ReadKey(); }
使用XDocument創(chuàng)建的Xml文件:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
本文標(biāo)題:C#使用XmlDocument或XDocument創(chuàng)建xml文件
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5048.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#獲取進(jìn)程或線程相關(guān)信息的方法
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載