欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 創(chuàng)建,讀取,寫入XML文件

 更新時間:2017年03月05日 10:27:44   作者:lengjing126  
本篇文章主要介紹了C# 創(chuàng)建,讀取,寫入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> 
 /// 創(chuàng)建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("創(chuàng)建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> 
 /// 創(chuàng)建節(jié)點 
 /// </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); 
  } 
 } 

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • C#中單問號(?)和雙問號(??)的用法整理

    C#中單問號(?)和雙問號(??)的用法整理

    本文詳細講解了C#中單問號(?)和雙問號(??)的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C#調(diào)用sql2000存儲過程方法小結(jié)

    C#調(diào)用sql2000存儲過程方法小結(jié)

    這篇文章主要介紹了C#調(diào)用sql2000存儲過程的方法,以實例形式分別對調(diào)用帶輸入?yún)?shù)及輸出參數(shù)的存儲過程進行了詳細分析,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • 一步步教你如何創(chuàng)建第一個C#項目

    一步步教你如何創(chuàng)建第一個C#項目

    這篇文章主要給大家介紹了關(guān)于如何創(chuàng)建第一個C#項目的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-12-12
  • WinForm實現(xiàn)為TextBox設(shè)置水印文字功能

    WinForm實現(xiàn)為TextBox設(shè)置水印文字功能

    這篇文章主要介紹了WinForm實現(xiàn)為TextBox設(shè)置水印文字功能,很實用的一個技巧,需要的朋友可以參考下
    2014-08-08
  • C# 透明窗體制作實現(xiàn)方法比較分析

    C# 透明窗體制作實現(xiàn)方法比較分析

    制作透明窗體辦法有好幾種,各有優(yōu)缺點.我們先來看看C#本身提供的辦法:通過設(shè)置窗體的 TransparencyKey實現(xiàn),需要的朋友可以了解下
    2012-12-12
  • C#之Socket(套接字)通信

    C#之Socket(套接字)通信

    這篇文章介紹了C#之Socket(套接字)通信,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C# WPF上位機實現(xiàn)和下位機TCP通訊的方法

    C# WPF上位機實現(xiàn)和下位機TCP通訊的方法

    這篇文章主要介紹了C# WPF上位機實現(xiàn)和下位機TCP通訊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • c# 獲取字符串的字節(jié)數(shù)的方法

    c# 獲取字符串的字節(jié)數(shù)的方法

    本篇文章主要是對c#中獲取字符串的字節(jié)數(shù)的方法進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#?使用Fluent?API?創(chuàng)建自己的DSL(推薦)

    C#?使用Fluent?API?創(chuàng)建自己的DSL(推薦)

    DSL領(lǐng)域?qū)S谜Z言是描述特定領(lǐng)域問題的語言,聽起來很唬人,其實不是什么高深的東西,下面通過實例代碼介紹下C#?使用Fluent?API?創(chuàng)建自己的DSL,感興趣的朋友參考下吧
    2021-12-12
  • MessageBox的Buttons和三級聯(lián)動效果

    MessageBox的Buttons和三級聯(lián)動效果

    這篇文章主要介紹了MessageBox的Buttons和三級聯(lián)動的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11

最新評論