2011-06-02 14 views
10

Bu özel yapılandırma App.config dosyasından nasıl okunur? Bu özel yapılandırma App.config dosyasından nasıl okunur?

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

Aksine bundan daha

:

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

Bu cevaplar size tam olarak yardımcı olmazsa, daha fazla yardımcı olabilmemiz için lütfen ek bilgi sağlayın. – Haukman

cevap

31

üst öğesine (ve bir çocuk koleksiyonu öğe) içinde doğrudan oturmak için toplama elementleri sağlamak için, ConfigurationProperty yeniden tanımlamak gerekir.

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

Ve bir koleksiyon gibi:

[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; 
    } 
} 

Ben ebeveyn bölümü/öğesini tanımlamak gerekir:

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

Örneğin, bu reklam gibi bir koleksiyon öğesi var diyelim [ConfigurationProperty("", IsDefaultCollection = true)] özniteliğine dikkat edin.

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

yerine: o boş bir ad verilmesi ve varsayılan koleksiyonu olarak ayarlama beni gibi benim yapılandırma tanımlamanızı sağlar Kullanabileceğin düşünüyorum

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

Sen özel yapılandırma bölümleri okumak için System.Configuration.GetSection() yöntemini kullanabilirsiniz.

GetSection() bu yana

4

bir XML belgesi olarak yapılandırma dosyasını açın ve sonra için XPath kullanarak (bölümler çekin gerekecek standart yapılandırma dosyası biçimi değildir hakkında daha fazla bilgi sahibi için http://msdn.microsoft.com/en-us/library/system.configuration.configuration.getsection.aspx bakın örnek). Bu belgeyi açma:

// 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"); 

Umarım bu probleminizi çözer. Mutlu Kodlama. :)

İlgili konular