读写xml文件的2个小函数

所属分类: 网络编程 / XML/RSS 阅读数: 954
收藏 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
更多精彩内容其他人还在看

利用XMLSerializer将对象串行化到XML

利用XMLSerializer将对象串行化到XML
收藏 0 赞 0 分享

XML入门的常见问题(四)

XML入门的常见问题(四)
收藏 0 赞 0 分享

UDDI FAQs

UDDI FAQs
收藏 0 赞 0 分享

asp.net生成HTML

asp.net生成HTML
收藏 0 赞 0 分享

ASP.NET 2.0页面框架的几处变化

ASP.NET 2.0页面框架的几处变化
收藏 0 赞 0 分享

虚机服务中常见Asp.Net低级错误一览

虚机服务中常见Asp.Net低级错误一览
收藏 0 赞 0 分享

XMLHTTP资料

XMLHTTP资料
收藏 0 赞 0 分享

在 XSL/XSLT 中实现随机排序

在 XSL/XSLT 中实现随机排序
收藏 0 赞 0 分享

用XML和XSL来生成动态页面

用XML和XSL来生成动态页面
收藏 0 赞 0 分享

SXNA RSS Blog 聚合器程序

SXNA RSS Blog 聚合器程序
收藏 0 赞 0 分享
查看更多