C#建立讀取寫入XML檔案的執行個體代碼分享

來源:互聯網
上載者:User
本篇文章主要介紹了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);   }  }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.