读写xml文件的2个小函数
读写xml文件的2个小函数
发布时间:2016-12-29 来源:查字典编辑
摘要:要利用DOM来存取XML文件,你必须将XML文件连结到HTML网页上。#region读写xml文件的2个小函数,200542byhycpub...

要利用DOM来存取XML文件,你必须将XML文件连结到HTML网页上。

#region读写xml文件的2个小函数,200542byhyc

publicvoidSetXmlFileValue(stringxmlPath,stringAppKey,stringAppValue)//写xmlPath是文件路径+文件名,AppKey是KeyName,AppValue是Value

{

XmlDocumentxDoc=newXmlDocument();

xDoc.Load(xmlPath);

XmlNodexNode;

XmlElementxElem1;

XmlElementxElem2;

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);

}

publicvoidGetXmlFileValue(stringxmlPath,stringAppKey,refstringAppValue)//读xmlPath是文件路径+文件名,AppKey是KeyName,AppValue是Value

{

XmlDocumentxDoc=newXmlDocument();

xDoc.Load(xmlPath);

XmlNodexNode;

XmlElementxElem1;

xNode=xDoc.SelectSingleNode("//appSettings");

xElem1=(XmlElement)xNode.SelectSingleNode("//add[@key='"+AppKey+"']");

if(xElem1!=null)

{

AppValue=xElem1.GetAttribute("value");

}

else

{

//MessageBox.Show("Thereisnotanyinformation!");

}

}

#endregion

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新XML/RSS学习
热门XML/RSS学习
编程开发子分类