日期:2014-05-20  浏览次数:20958 次

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 "/>