读写xml文件的2个小函数

所属分类: 网络编程 / XML/RSS 阅读数: 916
收藏 0 赞 0 分享
要利用DOM 来存取XML 文件,你必须将XML 文件连结到HTML 网页上。

#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
更多精彩内容其他人还在看

用XML和SQL 2000来管理存储过程调用

用XML和SQL 2000来管理存储过程调用
收藏 0 赞 0 分享

把数据转换成XML格式的好处

把数据转换成XML格式的好处
收藏 0 赞 0 分享

使用XML实现BBS(主题列表篇)

使用XML实现BBS(主题列表篇)
收藏 0 赞 0 分享

为何XML对Web服务很重要

为何XML对Web服务很重要
收藏 0 赞 0 分享

利用XML开发留言板简单的例子

利用XML开发留言板简单的例子
收藏 0 赞 0 分享

xml中的空格之完全解说

xml中的空格之完全解说
收藏 0 赞 0 分享

如何快速通过XSL转换XML文件

如何快速通过XSL转换XML文件
收藏 0 赞 0 分享

XML数据库中几个容易混淆的概念

XML数据库中几个容易混淆的概念
收藏 0 赞 0 分享

XML卷之实战锦囊(1):动态排序

XML卷之实战锦囊(1):动态排序
收藏 0 赞 0 分享

XML卷之实战锦囊(2):动态查询

XML卷之实战锦囊(2):动态查询
收藏 0 赞 0 分享
查看更多