2016-04-26 36 views
0

當我嘗試讀取我的設置時出現錯誤。我知道我在某處寫錯了。由於我找不到太多信息,單獨完成該程序。設置xml StackOverflowException錯誤

//設置讀碼

public static class proSave 
    { 
     public static propertyClass oku 
     { 
      get 
      { 
       XmlSerializer serialize = new XmlSerializer(typeof(propertyClass)); 
       var stream = new StreamReader("settings.xml"); 
       return (propertyClass)serialize.Deserialize(stream); 

      } 
     } 
    } 

設置應用代碼

namespace property 
     { 
     [Serializable] 
     public class propertyClass 
     { 
      private string _serverName = proSave.oku.serverName; 
      [Description("Server Bağlantı Adı"), Category("Server Setting")] 
      public string serverName { get { return _serverName; }} 

      private string _databaseName = proSave.oku.serverName; 
      [Description("Server Bağlantısı Dosya Adı"), Category("Server Setting")] 
      public string databaseName { get { return _serverName; } } 

      private string _user = proSave.oku.user; 
      [Description("Server Bağlantısı kullanıcı adı"), Category("Server Setting")] 
      public string user { get { return _user; } } 

      private string _password = proSave.oku.password; 
      [Description("Server Bağlantısı kullanıcı şifresi"), Category("Server Setting")] 
      public string password { get { return _password; } } 

      private int _gridHeight = proSave.oku.gridHeight; 
      [Description("Grid Hücre Yükseklik Ayarı \nFormlar Yenilendiğinde etkinleştirilecektir."), Category("Grid Ayarları")] 
      public int gridHeight { get { return _gridHeight; } } 


     } 
    } 

XML:異常

<?xml version="1.0" encoding="utf-8"?> 
<propertyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <serverName>1</serverName> 
    <databaseName>Kadir</databaseName> 
    <user>as</user> 
    <gridHeight>40</gridHeight> 
</propertyClass> 
+0

你得到的錯誤是什麼? – leppie

+0

您能提供一些有關您的settings.xml文件的錯誤和內容的詳細信息嗎? – abrown

回答

2

我想你的propertyClass中有一個循環引用,它引起一個循環,最終導致一個StackOverflow異常(在propertyClass中引用proSave.oku,它是propertyClass的一個實例)。刪除你propertyClass這個參考解決您的問題 - 可以保留你的代碼的其餘部分,因爲它是:

[Serializable] 
public class propertyClass 
{ 
    [Description("Server Bağlantı Adı"), Category("Server Setting")] 
    public string serverName { get; set; } 

    [Description("Server Bağlantısı Dosya Adı"), Category("Server Setting")] 
    public string databaseName { get; set; } 

    [Description("Server Bağlantısı kullanıcı adı"), Category("Server Setting")] 
    public string user { get; set; } 

    [Description("Server Bağlantısı kullanıcı şifresi"), Category("Server Setting")] 
    public string password { get; set; } 

    [Description("Grid Hücre Yükseklik Ayarı \nFormlar Yenilendiğinde etkinleştirilecektir."), Category("Grid Ayarları")] 
    public int gridHeight { get; set; } 
} 

如果你想延伸,以防止讀取XML文件中的每個時刻的執行,你可以進一步修改有ProSave類「緩存」的設置,例如:

public static class proSave 
{ 
    private static propertyClass settings; 

    private const string SettingsFilePath = "C:\\settings.xml"; 

    public static propertyClass oku 
    { 
     get 
     { 
      if (settings == null) 
      { 
       settings = GetSettings(); 
       return settings; 
      } 

      return settings; 
     } 
    } 

    public static void SaveSettings(propertyClass settings) 
    { 
     XmlSerializer writer = new XmlSerializer(typeof(propertyClass)); 

     using (FileStream file = File.Create(SettingsFilePath)) 
     { 
      writer.Serialize(file, settings); 
     } 
    } 

    private static propertyClass GetSettings() 
    { 
     XmlSerializer serialize = new XmlSerializer(typeof(propertyClass)); 

     using (var stream = new StreamReader(SettingsFilePath)) 
     { 
      return (propertyClass)serialize.Deserialize(stream); 
     } 
    } 
} 

在SaveSettings寫設置時回()方法,您將需要處理的IOExceptions如果該文件被鎖定。

您還可以查看替代方案,例如使用內置的ConfigurationManager或實現自定義ConfigurationSection。希望這可以幫助。

+0

我如何遵循大部分讀寫操作的路徑? –

+0

對不起,我不明白你的問題 - 你能否提供更多的細節,謝謝。 – abrown

+0

還有一種寫作和閱讀操作的方法可以做到自動設置嗎?這裏的目標是不斷打開和關閉文件,以獲益的方式提供安慰。我正在用谷歌翻譯寫作。 –

0
<?xml version="1.0" encoding="utf-8"?> 
<propertyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <serverName>1</serverName> 
    <databaseName>Kadir</databaseName> 
    <user>as</user> 
    <gridHeight>40</gridHeight> 
</propertyClass> 

System.StackOverflowException'類型的拋出。

+0

使用問題下方的[編輯]鏈接添加其他信息。不要將它寫入答案框 - 只用於回答您提問的問題。堆棧溢出不是一個典型的「論壇」 - 任何(最小)的討論都應該在評論中進行。 –