当前位置: 移动技术网 > IT编程>开发语言>c# > C# winfrom实现读取修改xml

C# winfrom实现读取修改xml

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文示例为大家分享了winfrom实现读取修改xml的具体代码,供大家参考,具体内容如下

在winfrom窗体中放一个文本框,2个按钮,一个panle,如下图


form.cs文件中的代码:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.xml;


namespace xmlconfiger
{
 public partial class form1 : form
 {
  public form1()
  {
   initializecomponent();
  }
   public string path;
   xmlconfig xmlconfig;
  //读取xml内容
  private void button1_click(object sender, eventargs e)
  {


   openfiledialog filename = new openfiledialog();//定义一个文件打开控件
   filename.initialdirectory = application.startuppath;//设置打开控件后,默认目录为exe运行文件所在文件夹
   filename.filter = "所有xml文件|*.xml";//设置控件打开的文件类型
   filename.filterindex = 2;//设置控件打开文件类型的显示顺序
   filename.restoredirectory = true;//设置对话框是否记忆之前打开的目录
   if (filename.showdialog() == dialogresult.ok)
   {
     path = filename.filename.tostring();//获得用户选择的完整路径
     name = path.substring(path.lastindexof("\\") + 1);//获取用户选择的不带路径的文件名
     xmlconfig = new xmlconfig(path);
     int count = xmlconfig.getcount();
     int ysplit = 30;
     int x1 = 3;
     for (int i = 0; i < count; i++)
     {
       label lb = new label();
       lb.text = xmlconfig.getname(i).tostring();
       lb.tag = "";
       lb.size = new system.drawing.size(60, 23);
       lb.autosize = false;
       textbox tb = new textbox();
       tb.text = xmlconfig.getxmlnode(i).tostring();
       tb.tag = i;
       lb.location = new point(x1, i * ysplit);
       tb.location = new point(x1 + lb.size.width + 10, i * ysplit);
       panel1.controls.add(lb);
       panel1.controls.add(tb);
      
     }


     
  
   }
  }
  //修改xml内容
  private void button2_click(object sender, eventargs e)
  {
   for (int i = 0; i < this.panel1.controls.count; i++)
   {
    if (this.panel1.controls[i].tag != null && this.panel1.controls[i].tag.tostring() != "")
     {
      textbox textbox1 = (textbox)(this.panel1.controls[i]);
      xmlconfig.savaxmlconfig(convert.toint32(textbox1.tag), textbox1.text);
     }
   }


   xmlconfig.savaconfig();
  }

  
 }
}

xmlconfig.cs中的代码:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.xml;
using system.io;
using system.data;
using system.windows.forms;


namespace xmlconfiger
{
 public class xmlconfig
 {
  public int count = 0;
  public string path="";
  private list<string> strlist = new list<string>();
  private list<string> listname = new list<string>();
  //构造函数获得所有信息
  public xmlconfig(string path)
  {
   xmldocument xmldoc = new xmldocument();
   xmldoc.load(path);//读取指定的xml文档 
   path = path;
   xmlnode roomlist = xmldoc.selectsinglenode("rss");
   xmlnodelist list = roomlist.childnodes;
   foreach (xmlnode item in list)
   {
    listname.add(item.attributes["name"].value);
    strlist.add(item.innertext);
    count = listname.count;
   }
  
  }
  //获取所有节点的个数
  public int getcount()
  {
   return count;
  }
  //通过tag值获取当前返回的name
  public string getname(int tag)
  {
   return listname[tag];
  }
  //通过tag值获取当前返回的value
  public string getxmlnode(int tag)
  {
   return strlist[tag];
  }
  //修改xml中所有的内容
  public void savaconfig()
  {
   xmldocument xmldoc = new xmldocument();
   xmldoc.load(path);
   xmlnodelist nodelist=xmldoc.selectsinglenode("rss").childnodes;//获取节点的所有子节点 
   for (int i = 0; i < nodelist.count; i++)//遍历所有子节点 
   {
    xmlelement xe = (xmlelement)nodelist[i];
    xmlnode childxml = nodelist[i];
    for (int j = 0; j < strlist.count; j++)
    {
     if (listname[j] == childxml.attributes["name"].value)
     {
      xe.setattribute("name", listname[i]);
      xe.innertext = strlist[i];
      break;
     }


    }


   }
   xmldoc.save(path);//保存。 
  }
  //修改xml中某一个节点
  public void savaxmlconfig(int tag, string name)
  {
   strlist[tag] = name;
  } 
  


 }
}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
 <student name="姓名">宁泽涛</student>
 <age name="年龄">22</age>
 <hobby name="爱好">游泳</hobby>
</rss>

以上就是本文的全部内容,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网