本篇文章主要介紹了C# 創建,讀取,寫入XML檔案的方法,具有很好的參考價值。下面跟著小編一起來看下吧
Xml檔如下:
<?xml version="1.0" encoding="utf-8"?> <Advertisements> <Ad> <ImageUrl>001.jpg</ImageUrl> <NavigateUrl>001.aspx</NavigateUrl> <Impressions>10</Impressions> <Keyword>gucas01</Keyword> </Ad> <Ad> <ImageUrl>002.jpg</ImageUrl> <NavigateUrl>002.aspx</NavigateUrl> <Impressions>20</Impressions> <Keyword>gucas02</Keyword> </Ad> <Ad> <ImageUrl>003.jpg</ImageUrl> <NavigateUrl>003.aspx</NavigateUrl> <Impressions>30</Impressions> <Keyword>gucas03</Keyword> </Ad> <Ad> <ImageUrl>004.jpg</ImageUrl> <NavigateUrl>004.aspx</NavigateUrl> <Impressions>40</Impressions> <Keyword>gucas04</Keyword> </Ad> <Ad> <ImageUrl>005.jpg</ImageUrl> <NavigateUrl>005.aspx</NavigateUrl> <Impressions>50</Impressions> <Keyword>gucas05</Keyword> </Ad> <Ad> <ImageUrl>google.gif</ImageUrl> <NavigateUrl>http://www.google.com</NavigateUrl> <Impressions>30</Impressions> <Keyword>google</Keyword> </Ad> </Advertisements>
對XML檔的動作
/// <summary> /// 创建XML文件 /// </summary> /// <param name="filename"></param> public void CreateXmlFile(string filename) { XmlDocument xmldoc = new XmlDocument(); XmlNode node; node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); xmldoc.AppendChild(node); XmlNode root = xmldoc.CreateElement("Users"); xmldoc.AppendChild(root); CreateNode(xmldoc, root, "UserName", "zhengyd"); CreateNode(xmldoc, root, "Email", "zhengyd@gucas.ac,cn"); CreateNode(xmldoc, root, "Url", "www.gucas.an,cn"); CreateNode(xmldoc, root, "Age", "27"); try { xmldoc.Save(Server.MapPath(filename)); Response.Write("创建XML文件myxml.xml成功!"); } catch (Exception ex) { Response.Write(ex.Message); } } /// <summary> /// 写入XML文件 /// </summary> /// <param name="file"></param> public void WriteXmlFile(string file) { XmlDocument xmdoc = new XmlDocument(); try { xmdoc.Load(Server.MapPath(file)); XmlNode root = xmdoc.SelectSingleNode("Advertisements"); if (root != null) { XmlNode node = xmdoc.CreateNode(XmlNodeType.Element, "Ad", null); CreateNode(xmdoc, node, "ImageUrl", "google.gif"); CreateNode(xmdoc, node, "NavigateUrl", "http://www.google.com"); CreateNode(xmdoc, node, "Impressions", "30"); CreateNode(xmdoc, node, "Keyword", "google"); root.AppendChild(node); } xmdoc.Save(Server.MapPath(file)); Response.Write("写入XML文件XMLFile.xml成功。<br>"); } catch(Exception ex) { Response.Write(ex.Message); } } /// <summary> /// 创建节点 /// </summary> /// <param name="xmldoc"></param> /// <param name="parentnode"></param> /// <param name="name"></param> /// <param name="value"></param> public void CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value) { XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentnode.AppendChild(node); } /// <summary> /// 读取XML文件 /// </summary> /// <param name="file"></param> private void ReadFile(string file) { XmlDocument xmldoc = new XmlDocument(); try { xmldoc.Load(Server.MapPath(file)); XmlNode node = xmldoc.SelectSingleNode("Advertisements"); if (node != null) { TreeNode root = new TreeNode(); root.Text = node.Name; tvXml.Nodes.Add(root); foreach (XmlNode xnode in xmldoc.SelectNodes("Advertisements/Ad")) { TreeNode tnode = new TreeNode(); tnode.Text = Server.HtmlEncode("<" + xnode.Name + ">"); root.ChildNodes.Add(tnode); foreach (XmlNode xcnode in xnode.ChildNodes) { TreeNode tcnode = new TreeNode(); tcnode.Text = Server.HtmlEncode("<" + xcnode.Name + ">" + xcnode.InnerText + "</" + xcnode.Name + ">"); tnode.ChildNodes.Add(tcnode); } TreeNode ttnode = new TreeNode(); ttnode.Text = Server.HtmlEncode("</" + xnode.Name + ""); root.ChildNodes.Add(ttnode); } TreeNode tpnode = new TreeNode(); tpnode.Text = Server.HtmlEncode("</" + node.Name + ">"); tvXml.Nodes.Add(tpnode); } } catch (Exception ex) { Response.Write(ex.Message); } }
以上是C#建立讀取寫入XML檔案的實例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

C#.NET面試問題和答案包括基礎知識、核心概念和高級用法。 1)基礎知識:C#是微軟開發的面向對象語言,主要用於.NET框架。 2)核心概念:委託和事件允許動態綁定方法,LINQ提供強大查詢功能。 3)高級用法:異步編程提高響應性,表達式樹用於動態代碼構建。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。