首页 > 编程 > .NET > 正文

ASP.NET读写XML文件的示例代码

2024-07-10 13:13:08
字体:
来源:转载
供稿:网友
 public void setxmlfilevalue(string xmlpath,string appkey,string appvalue)//写xmlpath是文件路径+文件名,appkey是 key name,appvalue是value
  {
   xmldocument xdoc = new xmldocument();
   xdoc.load(xmlpath);
   xmlnode xnode;
   xmlelement xelem1;
   xmlelement xelem2;

   xnode =  xdoc.selectsinglenode("//appsettings");

   xelem1 = (xmlelement)xnode.selectsinglenode("//add[@key='" + appkey + "']");
   if ( xelem1 != null )
   {
    xelem1.setattribute("value",appvalue);
   }
   else
   {
    xelem2 = xdoc.createelement("add");
    xelem2.setattribute("key",appkey);
    xelem2.setattribute("value",appvalue);
    xnode.appendchild(xelem2);
   }
   xdoc.save(xmlpath);
  }


  public void getxmlfilevalue(string xmlpath,string appkey,ref string appvalue)//读xmlpath是文件路径+文件名,appkey是 key name,appvalue是value
  {
   xmldocument xdoc = new xmldocument();
   xdoc.load(xmlpath);
   xmlnode xnode;
   xmlelement xelem1;

   xnode =  xdoc.selectsinglenode("//appsettings");

   xelem1 = (xmlelement)xnode.selectsinglenode("//add[@key='" + appkey + "']");
   if ( xelem1 != null )
   {
    appvalue=xelem1.getattribute ("value");
   }
   else
   {
//    messagebox.show ("there is not any information!");
   }

  }

  #endregion

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表