欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 创建,读取,写入XML文件

程序员文章站 2022-06-20 16:11:30
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); 
  } 
 } 

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!