2016-02-05 81 views
-2

我有一個XML文件,我想基於從下拉列表中選擇的值動態更新。因此,這裏是我試過的邏輯,錯誤 - 不支持異常未被用戶代碼處理

讀取和更新XML: -

private void ReadandUpdateXML(string CMEURL) 
     { 
      XmlDocument doc = new XmlDocument(); 
      string inputxmlPath = ConfigurationManager.AppSettings["InputDataFileName"]; 
      doc.Load(inputxmlPath); 
      var ParentNode = doc.SelectSingleNode("//TestData"); 
      if (ParentNode.ChildNodes.Count > 0) 
      { 
       foreach (XmlNode child in ParentNode) 
       { 
        for (int i = 0; i < child.ChildNodes.Count; i++) 
        { 
         child.ChildNodes[i].InnerText = CMEURL; 
        } 
       } 
       doc.Save(inputxmlPath); 
      } 
     } 

我得到了更新XML值後一個要求,我需要閱讀

讀取XML : -

private static List<TestData> GetInputData(string NodeName) 
     { 
      string InputFilePath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["InputDataFileName"];// "InputData.xml"; 
      Stream inputStream = File.Open(InputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);// getting exception here 
      XElement xelement = XElement.Load(inputStream); 
      var name = from nm in xelement.Elements(NodeName).Descendants() 
         select new 
         { 
          Input = System.Text.RegularExpressions.Regex.Replace(nm.Value, @"\t|\n|\r", "").Trim(), 
          Result = (nm.Attribute("Result") == null ? string.Empty : System.Text.RegularExpressions.Regex.Replace(nm.Attribute("Result").Value, @"\t|\n|\r", "").Trim()) 
         }; 

      List<TestData> tdList = new List<TestData>(); 
      foreach (var item in name.ToList()) 
      { 
       if (!string.IsNullOrEmpty(item.Input)) 
       { 
        TestData td = new TestData(); 
        td.Input = item.Input; 
        td.Result = item.Result; 
        tdList.Add(td); 
       } 
      } 

      return tdList; 
     } 

Stream inputStream = File.Open(InputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); // Getting exception here 

例外: - 不支持例外是由用戶代碼未處理。給定路徑的格式不受支持。

讀取XML邏輯之前工作正常..但在更新xml後,它拋出一個錯誤。 不知道我做錯了什麼.. 任何人都可以幫助我嗎?

+2

請顯示[mcve]顯示問題,並且包含完整的堆棧跟蹤 - 目前我們不知道哪部分代碼拋出異常。 –

回答

0

夥計!我發現我的問題的解決方案..

在web.config中: - 我得到的文件路徑類似下面

<add key="InputDataFileName" value="C:\Users\vishnu\Desktop\PQC project\source\Tridion.AutomationDashboard\InputData.xml" /> 

而且我讀這樣的

string inputxmlPath = ConfigurationManager.AppSettings["InputDataFileName"]; 

而且在讀取XML的代碼,我收到文件路徑的基本目錄這樣

string InputFilePath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["InputDataFileName"]; 

所以InputFilePath被追加了兩次。這就是爲什麼它會拋出一個錯誤。

我的不好,我沒有告訴任何有關web.config文件。

相關問題