当前位置: 首页 > 图文教程 > XML家族 > XML > 读写xml文件的2个小函数

XML
XML 中的常见问题(二)
XML轻松学习手册(1)XML快速入门
XML入门的常见问题(三)
XML入门的常见问题(四)
XML轻松学习手册(2)XML概念
轻松学习手册(3)XML的术语
XML轻松学习手册(4)XML语法
XML轻松学习手册(5)XML实例解析
XML卷之实战锦囊(1):动态排序
XML卷之实战锦囊(2):动态查询
XML卷之实战锦囊(3):动态分页
XML卷之实战锦囊(4):选单连动
XML卷之实战锦囊(5):结构树图
对XML数据使用XMLConvert
XSL基础教程第一章
XSL基础教程第二章
XSL基础教程第三章
XSL基础教程第四章
XSL基础教程第五章
具体实现 XML 的三种方式

XML 中的 读写xml文件的2个小函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 113 ::
收藏到网摘: n/a

#region 读写xml文件的2个小函数,2005 4 2 by hyc

  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