As we all know, some of the flexibility of the program is "configuration.
Of course, the config file has never been easy for. Net users. At least, I think it is very troublesome.
1. config
. Net configuration file is convenient, in fact, the most convenient is the deleettings
<appSettings> <add key="apiKey" value = "8asdf9as9d2asd" /></appSettings>
User-Defined configuration is cumbersome. You must be familiar with the. NET configuration file system to understand it.
Of course, the people who are dedicated provide a DSL masterpiece-configurationsectiondesigner.
To some extent, my custom configuration for. NET is contradictory:
I like it because it is strict and cannot be modified;
I don't like it. It is troublesome to modify the design, so I need to make a lot of references.
2. xml
XML is very flexible, but I am afraid of its complicated APIs. I have been holding this attitude until I used the LINQ to XML.
I even want to write an xmlhelper multiple times. It's silly to see a bunch of definitions and APIs each time.
Here, I changed my attitude towards XML with the help of LINQ to XML.
Main APIs:
1) load a document;
Xdocument Doc = xdocument. Load (PATH); or xelement Doc = xelement. Load (PATH );
The difference is that after xelement is loaded, xdocument is treated as an element, while xdocument has a typical attribute-root.
2) Search for elements;
It provides a good tree-like API that gives people a clear feeling.
Doc. elements ("yourname"); // search for the Element Set Doc. element ("yourname"); // find a single element Doc. descendants ("yourname"); // search for child element
Of course, it is natural to add the extension of LINQ, where, select, and other Lambda methods.
3) modify, add, and delete elements;
Setvalue, add, addafterself, addbeforeself
Removeall
XElement root = new XElement("Categories", new XElement("Category", new XAttribute("CategoryID", "1"), new XElement("CategoryName", "Beverages"), new XElement("Description", "Soft drinks, coffees, teas, beers, and ales") ) ); root.Element("Category").Add(new XAttribute("AddDate", DateTime.Now.ToShortDateString())); root.Save(path);
It is so convenient to read and write XML files.
3. Select
In general, XML is more flexible than the config file.
I tend to use XML for configuration when config cannot cope with various complex configurations (code modification is helpless;
When the configuration is fixed, I prefer to use config for configuration.
When using an XML file, there is a vs prompt-select the XML file, right-click the file, properties, output directory, and select "always copy ".
For configuration file trade-offs,. config vs. xml