欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【读写xml文件的2个小函数】,下面是详细的分享!
读写xml文件的2个小函数
#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文件的2个小函数,下面是编辑为你推荐的有价值的用户互动:
相关问题:怎么用C语言写一个简单的XML文件
答:1、XML文件和普通的txt文本文件并无区别,所以用普通的文件操作函数fopen,fsacnf,fprintf即可读写XML文件。 2、例程: #include #include int main() { FILE *fp1; char get[1000],ch; int i; fp1=fopen("test.xml","r+");//以读写方式打开或者... >>详细
相关问题:如何使用libxml2计算一个xml文件的大小?
答:1、xmlDoc是一个struct,保存了一个xml的相关信息;xmlDocPtr等于xmlDoc*。 2、计算这个struct 的大小*个数: xml文件的大小 = sizeof( struct xmlDoc)*(XML个数) >>详细
相关问题:C# XMl读写配置文件
答: 192.168.0.1 30 60 3000一般像这样就行了 很简单的 或者多个配置你可以加个item标签如: 192.168.0.1 30 60 3000 192.168.0.2 30 60 3000 假定xml文件路径为:path 读取和保存: using system.xml;XmlDocument xml = new XmlDocument();//声明x... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
