程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 創建,讀取,寫入XML文件

C# 創建,讀取,寫入XML文件

編輯:C#入門知識

C# 創建,讀取,寫入XML文件。本站提示廣大學習愛好者:(C# 創建,讀取,寫入XML文件)文章只能為提供參考,不一定能成為您想要的結果。以下是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", "[email protected],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); 
  } 
 } 

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持!

[db:作者簡介][db:原文翻譯及解析]
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved