Efficient approach to load XML and read it when needed using c#/.net 3.5
I want to know the efficient way to store the configuration xml to be used in the program. I want to load the following config xml into memory as soon as I start the program and then use the properties where needed. XML:
<ViewModelConfiguration>
<FICallSchedule>
<Model>
<Details>
<DataSource>
<Dataset se-datafilter="callschedule" dv-datamanipulationrequired="false" dv-filtercondition="" dv-sortcolumn="" dv-gettopNrows="" />
<XmlData></XmlData>
</DataSource>
<ComputePercentage isactive="true" dataorientation="horizontal">
<column source="value1" destination="value3" datafilter="" />
<column source="value2" destination="value4" datafilter="" />
</ComputePercentage>
</Details>
</Model>
</FICallSchedule>
</ViewModelConfiguration>
Currently I am reading the main tags like <Dataset>
and load all the attributes of it in dictionary object. And then use that dictionary with key in my code. Similarly I do it for ComputePercentage and so on and so forth.
I was wondering i开发者_如何学Cf I can load the complete XML into some object and access each node or attribute something like: Model.Details.Dataset.DataSource.se-datafilter
or Model.Details.ComputePercentage
which will return a collection of columns. I am not sure if this makes any sense otherwise I would stick to dictionary objects only.
If the configuration file has some predefined schema - it stands to reason that it does - you can use the xsd tool to generate a class based on that schema. There is also a free xsd2code library that does similar thing.
But you have to define your Model
class somehow; either by autogenerating it with the aid of these tools, or by implementing it "manually".
Take use of configSection
and configElement
, that way elements and attributes will be loaded as object, and this configuration can be stored into application config file.
精彩评论