2009-01-15 20 views
10

पढ़ने के लिए जेनेरिक विधि एक कॉन्फ़िगरेशन फ़ाइल से अनुभाग पढ़ने के लिए सामान्य तरीका लागू करने का प्रयास कर रहा हूं। कॉन्फ़िगरेशन फ़ाइल में नीचे 'मानक' अनुभाग या 'कस्टम' अनुभाग हो सकते हैं।कॉन्फ़िगरेशन अनुभाग

<configuration> 
<configSections> 
    <section name="NoteSettings" type="System.Configuration.NameValueSectionHandler"/> 
</configSections> 
<appSettings> 
    <add key="AutoStart" value="true"/> 
    <add key="Font" value="Verdana"/> 
</appSettings> 
<NoteSettings> 
    <add key="Height" value="100"/> 
    <add key="Width" value="200"/> 
</NoteSettings> 

विधि है कि मैंने कोशिश की इस प्रकार है:

private string ReadAllSections() 
    { 
     StringBuilder configSettings = new StringBuilder(); 

     Configuration configFile = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); 

     foreach (ConfigurationSection section in configFile.Sections) 
     { 
      configSettings.Append(section.SectionInformation.Name); 
      configSettings.Append(Environment.NewLine);     

      if (section.GetType() == typeof(DefaultSection)) 
      { 
       NameValueCollection sectionSettings = ConfigurationManager.GetSection(section.SectionInformation.Name) as NameValueCollection; 

       if (sectionSettings != null) 
       { 
        foreach (string key in sectionSettings) 
        { 
         configSettings.Append(key); 
         configSettings.Append(" : "); 
         configSettings.Append(sectionSettings[key]); 
         configSettings.Append(Environment.NewLine); 
        } 
       } 
      } 

      configSettings.Append(Environment.NewLine); 
     } 

     return configSettings.ToString(); 
    } 

यह मानते हुए कि सभी कस्टम वर्गों केवल मुख्य मान होगा

  • इस प्रकार का कार्यान्वयन है मुमकिन? और यदि हां, तो क्या इस से एक 'क्लीनर' और अधिक सुरुचिपूर्ण समाधान है?
  • उपर्युक्त विधि 'अदृश्य' अनुभाग भी पढ़ती है जैसे mscorlib, system.diagnostics। क्या यह टालने योग्य है?
  • System.Data.Dataset एक डेटासेट देता है जिसे NameValueCollection पर नहीं डाला जा सकता है। इसे कैसे संभाला जा सकता है?

सुधार/सुझाव स्वागत है।

धन्यवाद।

उत्तर

9

के बाद से विन्यास फाइल एक्सएमएल फ़ाइल है, तो आप XPath प्रश्नों इस कार्य के लिए उपयोग कर सकते हैं:

Configuration configFile = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location); 
    XmlDocument document = new XmlDocument(); 
    document.Load(configFile.FilePath); 
    foreach (XmlNode node in document.SelectNodes("//add")) 
    { 
     string key = node.SelectSingleNode("@key").Value; 
     string value = node.SelectSingleNode("@value").Value; 
     Console.WriteLine("{0} = {1}", key, value); 
    } 

आप सभी प्राप्त करने के लिए की जरूरत है {कुंजी, मूल्य} जोड़ी तो आपको XPath प्रश्नों के तीन गुना परिभाषित करने की आवश्यकता है: 1 - समान संरचना वाले नोड्स का चयन करने के लिए मुख्य क्वेरी। 2, 3 - पहली क्वेरी द्वारा पुनर्प्राप्त नोड्स से कुंजी और मूल्य नोड निकालने के लिए क्वेरी। आपके मामले में सभी नोड्स के लिए सामान्य क्वेरी होना पर्याप्त है, लेकिन विभिन्न कस्टम अनुभागों के लिए समर्थन बनाए रखना आसान है।

2

अपनी कॉन्फ़िगरेशन को XmlDocument में पढ़ें और फिर तत्वों को ढूंढने के लिए XPath का उपयोग करें?

कुछ ऐसा;

XmlDocument doc = new XmlDocument(); 
doc.Load(HttpContext.Current.Server.MapPath("~/web.config")); 

XmlNodeList list = doc.SelectNodes("//configuration/appSettings"); 

foreach (XmlNode node in list[0].ChildNodes) 

...

1

जब आप एक वर्ग के लिए प्रकार विशेषता के रूप में NameValueSectionHandler निर्दिष्ट किया है और Configuration.GetSection(string) करने के लिए कॉल, आप वापसी प्रकार के रूप में एक DefaultSection उदाहरण प्राप्त होगा।

string SectionInformation.SectionInformation.GetRawXml() इस मामले में आपके डेटा में जाने के लिए महत्वपूर्ण है।

मैंने System.Configuration का उपयोग करके इसे करने के वैध तरीके के साथ एक और समान प्रश्न का उत्तर दिया कि आप सभी विवरण और कोड स्निपेट प्राप्त करने के संदर्भ में संदर्भित कर सकते हैं। NameValueSectionHandler can i use this section type for writing back to the app

2

आप इस प्रकार कोई कस्टम खंड पढ़ सकते हैं:

var sectionInformation = configuration.GetSection("mysection").SectionInformation; 
var xml = sectionInformation.GetRawXml(); 
var doc = new XmlDocument(); 
doc.LoadXml(xml); 
IConfigurationSectionHandler handler = (IConfigurationSectionHandler)Type.GetType(sectionInformation.Type).GetConstructor(new Type[0]).Invoke(new object[0]); 
var result = handler.Create(null, null, doc.DocumentElement); 
+0

यह महान काम किया! – codechurn

संबंधित मुद्दे