Thứ Năm, 13 tháng 9, 2007

Convert INI file to XML

private string GetConfigValue( XmlDocument xmlDoc, string sectionName, 
string settingName )
{
string valueRet;

// get setting node
string xpath =
String.Format( "//section[@name='{0}']/setting[@name='{1}']",
sectionName, settingName );
XmlNode node = xmlDoc.DocumentElement.SelectSingleNode( xpath );

// display value
if ( node == null )
{
throw new Exception(
String.Format(
"No such setting, using the following xpath:{0}{1}",
Environment.NewLine, xpath ) );
}
else
{
XmlAttribute xmlAttr = node.Attributes["value"];
if ( xmlAttr == null )
{
throw new Exception( String.Format(
"No value for this setting, using the following xpath:{0}{1}",
Environment.NewLine, xpath ) );
}
else
{
valueRet = xmlAttr.Value;
}
}

return valueRet;
}

// Load configuration from xml file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load( "C:\\WINNT\\iexplore.xml" );
string homePage = GetConfigValue( xmlDoc, "main", "Home Page" );
string searchPage = GetConfigValue( xmlDoc, "main", "Search Page" );

Không có nhận xét nào: