One may use XmlTextWriter (XmlTextReader) to write to (read from) an XML file. In .NET Framework 2.0, the recommended practice is to create XmlWriter instances using the System.Xml.XmlWriter.Create method and the XmlWriterSettings class. The reason of the replacement can be found in http://msdn2.microsoft.com/en-us/library/system.xml.xmltextwriter.aspx.
However, it is hard to read and write to an XML file at the same time. Thus in order to read and write a configuration XML file, one needs to use ConfigurationManager class that allows you to access machine, application, and user configuration information, such as “app.config”. The class allows you to read/write the configuration file at the same time. While using OpenExeConfiguration to open a configuration file, an issue about using mock .exe file is discussed in http://msdn2.microsoft.com/en-us/library/ms224437.aspx. To access configuration information, call the GetSection method, for example “TestConfigurationSection testSettingSection = (TestConfigurationSection)testConfig.GetSection(“microsoft.visualstudio.testtools”);”. If you want to update the file, use “testConfig.Save(ConfigurationSaveMode.Modified);”. Please note that Microsoft.VisualStudio.TestTools.UnitTesting namespace provides a TestConfigurationSection type. A good reference on this topic can be found at http://www.codeproject.com/csharp/ReadWriteXmlIni.asp.
May 21, 2007
Handling XML and configuration files
Posted by kevin3stone under Uncategorized | Tags: .NET, configuration, file handling, programming, unit test, VSTS, XML |Leave a Comment