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

XML
XML:XSL 样式单文档
XML:小编浅谈XML中的CSS
了解WEB页面工具语言XML(三)支持工具
了解WEB页面工具语言XML(四)应用分类
了解WEB页面工具语言XML(一)产生背景
了解WEB页面工具语言XML(二)定义
XML基础:什么是XML?
xml和html的不同之处
XML与Web服务和SOA有何关联?
XML模式:Dublin Core
XML模式:DocBook XM
XML模式:RDF
XML模式:vCard
XML模式:WSD
XML模式相关常用的缩写词
XML模式:SOAP
XML 语法
什么是XSL?
网页制作关于代码的18个小技巧
什么是XML?

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 112 ::
收藏到网摘: 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