2011-06-02 13 views
10

App.config से इस कस्टम कॉन्फ़िगरेशन को कैसे पढ़ा जाए?App.config से इस कस्टम कॉन्फ़िगरेशन को कैसे पढ़ा जाए?

<root name="myRoot" type="rootType"> 
    <element name="myName" type="myType" /> 
    <element name="hisName" type="hisType" /> 
    <element name="yourName" type="yourType" /> 
    </root> 

बल्कि इस से:

<root name="myRoot" type="rootType"> 
    <elements> 
    <element name="myName" type="myType" /> 
    <element name="hisName" type="hisType" /> 
    <element name="yourName" type="yourType" /> 
    </elements> 
    </root> 
+1

यदि ये उत्तर पूरी तरह से आपकी सहायता नहीं करते हैं, तो कृपया अतिरिक्त जानकारी प्रदान करें ताकि हम और सहायता कर सकें। – Haukman

उत्तर

31

पेरेंट तत्व (और नहीं एक बच्चे संग्रह तत्व) के भीतर सीधे बैठने के लिए अपने संग्रह तत्वों सक्षम करने के लिए, आप अपने ConfigurationProperty फिर से परिभाषित करने की जरूरत है।

public class TestConfigurationElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)] 
    public string Name 
    { 
     get { return (string)this["name"]; } 
    } 
} 

और एक संग्रह जैसे::

[ConfigurationCollection(typeof(TestConfigurationElement), AddItemName = "test")] 
public class TestConfigurationElementCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new TestConfigurationElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((TestConfigurationElement)element).Name; 
    } 
} 

मैं के रूप में माता पिता के अनुभाग/तत्व निर्धारित करने होंगे:

public class TestConfigurationSection : ConfigurationSection 
{ 
    [ConfigurationProperty("", IsDefaultCollection = true)] 
    public TestConfigurationElementCollection Tests 
    { 
     get { return (TestConfigurationElementCollection)this[""]; } 
    } 
} 

जैसे, मान लीजिए कि मैं इस तरह के रूप में एक संग्रह तत्व डालते हैं [ConfigurationProperty("", IsDefaultCollection = true)] विशेषता पर ध्यान दें।

<testConfig> 
    <test name="One" /> 
    <test name="Two" /> 
</testConfig> 
के बजाय

: यह एक खाली नाम देते हुए और डिफ़ॉल्ट संग्रह के रूप में सेट मुझे मेरे config की तरह परिभाषित करने के लिए अनुमति देता है

<testConfig> 
    <tests> 
    <test name="One" /> 
    <test name="Two" /> 
    </tests> 
</testConfig> 
7

आप कस्टम विन्यास वर्गों को पढ़ने के लिए System.Configuration.GetSection() विधि का उपयोग कर सकते हैं।

GetSection()

4

इस के बाद से मानक कॉन्फ़िग फ़ाइल स्वरूप आप एक XML दस्तावेज़ के रूप में config फ़ाइल को खोलने और उसके बाद (वर्गों बाहर खींच के लिए XPath का उपयोग कर करना होगा नहीं है के बारे में अधिक जानने के लिए http://msdn.microsoft.com/en-us/library/system.configuration.configuration.getsection.aspx का संदर्भ लें उदाहरण)। इस के साथ दस्तावेज़ खोलें:

// Load the app.config file 
XmlDocument xml = new XmlDocument(); 
xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 
0

मुझे लगता है कि आप उपयोग कर सकते

  XmlDocument appSettingsDoc = new XmlDocument(); 
      appSettingsDoc.Load(Assembly.GetExecutingAssembly().Location + ".config"); 
      XmlNode node = appSettingsDoc.SelectSingleNode("//appSettings"); 

      XmlElement element= (XmlElement)node.SelectSingleNode(string.Format("//add[@name='{0}']", "myname")); 
      string typeValue = element.GetAttribute("type"); 

उम्मीद है कि यह आपकी समस्या हल करता है। हैप्पी कोडिंग :)

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