2009-06-26 17 views
17

मान लीजिए कि मेरे पास एक कस्टम कॉन्फ़िगर फ़ाइल है जो कस्टम-डिफ़ाइंड कॉन्फ़िगरेशनसेक्शन और कॉन्फ़िगर तत्वों से मेल खाती है। ये कॉन्फ़िगरेशन क्लासेस लाइब्रेरी में संग्रहीत हैं।कॉन्फ़िगरेशन फ़ाइल को प्रोग्रामेटिक रूप से लोड करने के लिए कैसे करें

कॉन्फ़िग फ़ाइल इस

<?xml version="1.0" encoding="utf-8" ?> 
<Schoool Name="RT"> 
    <Student></Student> 
</Schoool> 

तरह लग रहा है मैं प्रोग्राम के रूप में कैसे लोड और कोड से कॉन्फ़िग फ़ाइल का उपयोग कर सकते हैं?

मैं कच्चे एक्सएमएल हैंडलिंग का उपयोग नहीं करना चाहता, लेकिन पहले से परिभाषित कॉन्फ़िगरेशन कक्षाओं का लाभ उठाना चाहता हूं।

+2

आप चयनित उत्तर क्यों नहीं चुनते? –

उत्तर

7

कोडप्रोजेक्ट पर .NET 2.0 कॉन्फ़िगरेशन पर जॉन रिस्टा की तीन-भाग श्रृंखला देखें।

अत्यधिक की सिफारिश की है, अच्छी तरह से लिखा और अत्यंत उपयोगी!

आप वास्तव में किसी भी XML खंड को लोड नहीं कर सकते - आप लोड एक पूर्ण, अलग कॉन्फ़िगरेशन फ़ाइल है जो app.config जैसा दिखता है और महसूस करता है।

आप बना सकते हैं और अपने स्वयं के कस्टम विन्यास वर्गों डिजाइन करने के लिए चाहते हैं, तो आप निश्चित रूप से भी CodePlex पर Configuration Section Designer की जांच करनी चाहिए - एक दृश्य स्टूडियो ऐड कि नेत्रहीन config वर्गों के लिए डिजाइन और सभी आवश्यक पाइपलाइन कोड के लिए उत्पन्न करने के लिए आप की अनुमति देता है आप।

+6

यह सहायक है लेकिन प्रश्न का उत्तर नहीं है। मैंने सभी तीन लेख पढ़े और मैं अभी भी बुद्धिमान नहीं हूं। –

+1

@ एंड्रयू मायहर: मैं यह कहने की कोशिश कर रहा था: अपने आप को घुमाने और पहिया का पुन: आविष्कार करने के बजाय - .NET ढांचे में पहले से ही उपलब्ध है। पहिया का पुन: आविष्कार करना बंद करो! वहाँ पहले से पर्याप्त पर्याप्त पहियों हैं - उनका उपयोग करें! –

2

configSource विशेषता आपको किसी भी कॉन्फ़िगरेशन तत्व को एक अलग फ़ाइल में स्थानांतरित करने की अनुमति देती है। आपका मुख्य app.config में आप कुछ इस तरह करना होगा:

<configuration> 
    <configSections> 
    <add name="schools" type="..." /> 
    </configSections> 

    <schools configSource="schools.config /> 
</configuration> 
+0

सच - लेकिन यहां तक ​​कि इस मामले में, आपको परिभाषा भाग के लिए अपना स्वयं का कस्टम कॉन्फ़िगरेशन तत्व बनाना होगा। –

+0

@marc_s: जैसा कि मैंने ओपी पढ़ा है, लेखक के पास पहले से ही "कस्टम-डिफ़ाइंड कॉन्फ़िगरेशनसेक्शन" है, लेकिन यह सुनिश्चित नहीं है कि इसे "कस्टम कॉन्फ़िगर फ़ाइल" के रूप में कैसे उपयोग किया जाए। हो सकता है मैं गलत हूं। –

+0

आप सही हो सकते हैं :-) मान लीजिए मैंने प्रश्न को सावधानीपूर्वक पर्याप्त नहीं पढ़ा - मी culpa। –

15

आप अपनी आवश्यकताओं के लिए यह अनुकूल करने के लिए होगा, लेकिन यहाँ कोड मैं अपनी परियोजनाओं में से एक में उपयोग सिर्फ इतना है कि करना है:

var fileMap = new ConfigurationFileMap("pathtoconfigfile"); 
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap); 
var sectionGroup = configuration.GetSectionGroup("applicationSettings"); // This is the section group name, change to your needs 
var section = (ClientSettingsSection)sectionGroup.Sections.Get("MyTarget.Namespace.Properties.Settings"); // This is the section name, change to your needs 
var setting = section.Settings.Get("SettingName"); // This is the setting name, change to your needs 
return setting.Value.ValueXml.InnerText; 

ध्यान दें कि मैं एक मान्य .net कॉन्फ़िगरेशन फ़ाइल पढ़ रहा हूं। मैं एक डीएलएल से EXE की कॉन्फ़िगरेशन फ़ाइल पढ़ने के लिए इस कोड का उपयोग कर रहा हूं। मुझे यकीन नहीं है कि यह आपके प्रश्न में दी गई उदाहरण कॉन्फ़िगरेशन फ़ाइल के साथ काम करता है, लेकिन यह एक अच्छी शुरुआत होनी चाहिए।

1

निम्नलिखित कोड आपको XML से ऑब्जेक्ट्स में सामग्री लोड करने की अनुमति देगा। स्रोत, app.config या किसी अन्य फ़ाइल के आधार पर, उपयुक्त लोडर का उपयोग करें।

using System.Collections.Generic; 
using System.Xml.Serialization; 
using System.Configuration; 
using System.IO; 
using System.Xml; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var section = SectionSchool.Load(); 
     var file = FileSchool.Load("School.xml"); 
    } 
} 

फ़ाइल लोडर:

public class FileSchool 
{ 
    public static School Load(string path) 
    { 
     var encoding = System.Text.Encoding.UTF8; 
     var serializer = new XmlSerializer(typeof(School)); 

     using (var stream = new StreamReader(path, encoding, false)) 
     { 
      using (var reader = new XmlTextReader(stream)) 
      { 
       return serializer.Deserialize(reader) as School; 
      } 
     } 
    } 
} 

धारा लोडर:

public class SectionSchool : ConfigurationSection 
{ 
    public School Content { get; set; } 

    protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey) 
    { 
     var serializer = new XmlSerializer(typeof(School)); // works in  4.0 
     // var serializer = new XmlSerializer(type, null, null, null, null); // works in 4.5.1 
     Content = (Schoool)serializer.Deserialize(reader); 
    } 
    public static School Load() 
    { 
     // refresh section to make sure that it will load 
     ConfigurationManager.RefreshSection("School"); 
     // will work only first time if not refreshed 
     var section = ConfigurationManager.GetSection("School") as SectionSchool; 

     if (section == null) 
      return null; 

     return section.Content; 
    } 
} 

डाटा परिभाषा:

[XmlRoot("School")] 
public class School 
{ 
    [XmlAttribute("Name")] 
    public string Name { get; set; } 

    [XmlElement("Student")] 
    public List<Student> Students { get; set; } 
} 

[XmlRoot("Student")] 
public class Student 
{ 
    [XmlAttribute("Index")] 
    public int Index { get; set; } 
} 

की ऐप की सामग्री।एक्सएमएल फ़ाइल के config '

<?xml version="1.0"?> 
<configuration> 

    <configSections> 
    <section name="School" type="SectionSchool, ConsoleApplication1"/> 
    </configSections> 

    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 

    <School Name="RT"> 
    <Student Index="1"></Student> 
    <Student /> 
    <Student /> 
    <Student /> 
    <Student Index="2"/> 
    <Student /> 
    <Student /> 
    <Student Index="3"/> 
    <Student Index="4"/> 
    </School> 

</configuration> 

सामग्री:

<?xml version="1.0" encoding="utf-8" ?> 
<School Name="RT"> 
    <Student Index="1"></Student> 
    <Student /> 
    <Student /> 
    <Student /> 
    <Student Index="2"/> 
    <Student /> 
    <Student /> 
    <Student Index="3"/> 
    <Student Index="4"/> 
</School> 

पोस्ट कोड विजुअल स्टूडियो 2010 (नेट 4.0) में चेक किया गया है। यह नेट 4.5.1 में काम करेंगे, तो आपके द्वारा दी गई नमूना बाहर डिबगर शुरू कर दिया है तो यह सबसे सरल निर्माता के साथ काम करेंगे तो

new XmlSerializer(typeof(School), null, null, null, null); 

को

new XmlSerializer(typeof(School)) 

से seriliazer के निर्माण के बदलने के लिए, लेकिन शुरू कर दिया है, तो डीएसगिंग के साथ वीएस2013 आईडीई से, फिर कन्स्ट्रक्टर में बदलाव की आवश्यकता होगी या फिर FileNotFoundException होगा (कम से कम मेरे मामले में था)।

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

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