读书人

C#中xml可以替代ini文件吗?解决方案

发布时间: 2012-03-30 17:32:09 作者: rapoo

C#中xml可以替代ini文件吗?
大家好,我想把我程序里的一些东西,保存下来,以便下次读取,本来写在ini文件里面,譬如
[APP1]
Title=Office
CopyRight=MS
Version=2K3
[APP2]
Title=SyamantecNortonVirus
CopyRight=Symantec
Version=10.0.2.2002

现在我想把这个内容放在一个xml文件里面,然后写个寒暑 ,fun(section,item)就可以找到具体某个值,譬如fun( "APP1 ", "Title "),就可以返回Office,不知道可以办到不?如果可以,怎么办到呢?分不多,请帮帮忙!

[解决办法]
using System.Xml;
using System.IO;


string FileName = HttpContext.Current.Server.MapPath( "路径 ");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(FileName);

//get all child nodes of BannerContent
XmlNodeList nodeList = xmlDoc.SelectSingleNode( "BannerContent ").ChildNodes;

//over all nodes
foreach (XmlNode xn in nodeList)
{
//change child node type to XmlElement
XmlElement xe = (XmlElement)xn;

if (xe.GetAttribute( "Name ") == "ChangeColor ")
{
if (xe.GetAttribute( "Visible ") == "true ")
{
if (us.GetFirstRoleId() != -1)
{
ChangeColorIcon = "changeicon ";
DesktopPortalBanner_ChangeColor = lc.GetLanguageDescription( "DesktopPortalBanner_ChangeColor ");
}
}
}
}



string filePathDesktopSrc = Server.MapPath( "路径 ");
if (!File.Exists(filePathDesktopSrc))
{
validateFileExist.ErrorMessage = BannerSrcPath.Text + " " +
lc.GetLanguageDescription( "ModuleDefinitions_Error ");
validateFileExist.IsValid = false;
return;
}

xmlDoc.Load(FileName);

//get all child nodes of BannerContent
XmlNodeList nodeList = xmlDoc.SelectSingleNode( "BannerContent ").ChildNodes;

#region over all nodes
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute( "Name ") == "ChangeColor ")
{
if (this.ManageChangeColor.Checked == true)
{
xe.SetAttribute( "Visible ", "true ");
}
else
{
xe.SetAttribute( "Visible ", "false ");
}
}
}
#endregion

xmlDoc.Save(FileName);//save xml file
[解决办法]
当然可以, 我都用xml做参数文件的。添加一个app.config,
<appSettings>
<add key= "SurroundingAreaIncluded " value= "0 "/>
<add key= "WaitPerPageSEC " value= "3333 " />
</appSettings>

在程序中ConfigurationManager.AppSettings.Get( "WaitPerPageSEC ").Trim();

当然可以自己新建xml
[解决办法]
以上帝之名保证可以
<root>
<APP1 Title= "Office " CopyRight= "MS " Version= "2K3 "/>


<APP2 Title= "SyamantecNortonVirus " CopyRight= "Symantec " Version= "10.0.2.2002 "/>
</root>
[解决办法]
可以呀,
Read/Write XML files, Config files, INI files, or the Registry
By Alvaro Mendez
http://www.codeproject.com/csharp/readwritexmlini.asp
[解决办法]
VS2005里,菜单:“项目=> (项目名)属性.. 打开项目属性下面有个 "设置 "。这里直接可以设置应用程序的属性,会自动生成和修改app.config,十分方便。
注意 "范围 ":Application则是只读的,User是运行时可修改的。
发布的时候注意加密配置文件中的敏感信息。
用户通过应用程序修改设置,设置会在
C:\Documents and Settings\用户名\Application Data\公司名
下生成一个user.config 保存用户修改的设置。
而且可以直接绑定到控件,控件里的数据=> ApplicationSettings属性里设置。
======================================
使用方法我举个例子:
=====================================
我的项目名是MyCSDN
我在项目属性设置一个名myStr,string类型, 值hello
程序里首先引用 :项目名.Properties;
例如,我们的:
using MyCSDN.Properties;

然后

Settings setting = new Settings();
this.textBox1.Text=setting.myStr;//读取配置myStr
setting.Save();//保存配置
setting.Reset();//还原默认值
setting.Reload();//读取最后一次保存的值
====================
如果是绑定到控件的,窗体建立的时候会自动读取,非常方便:)

读书人网 >C#

热点推荐