2017-03-08 147 views
0

我一直在爲這個管理應用程序工作數週,現在已經快完成了。當用戶點擊保存按鈕時應該發生什麼,調用VerifyFields()方法。之後,應該詢問用戶是否要保存併發送電子郵件。一旦用戶點擊是,它應該發送一封電子郵件到XML文件中的電子郵件地址。我收到錯誤消息「文件:DREmailAddresses.xml未找到:\ fs01 \ Applications \ EMS-Manager。」但是,該文件在那裏並且可以訪問。我已經複製了下面的相關代碼以及image of the error.通過網絡訪問XML文件

// Begin Email Section 
    public bool VerifyFields() 
    { 
     var status = false; 
     switch (tbxDynPartNumber.Text) 
     { 
      case "": 
       MessageBox.Show(@"Enter Dynalab Part#", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 
     switch (tbxSupplier.Text) 
     { 
      case "": 
       MessageBox.Show(@"Enter Supplier Name", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 
     switch (tbxInitiatedBy.Text) 
     { 
      case "": 
       MessageBox.Show(@"Enter Initiator Name", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 
     switch (orderQty.Text) 
     { 
      case "0": 
       MessageBox.Show(@"Order Quantity Must Be Greater Than 0!", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 

     switch (dtpDate.Text) 
     { 
      case "": 
       MessageBox.Show(@"Enter A Valid Date.", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 
     switch (tbxDescription.Text) 
     { 
      case "": 
       MessageBox.Show(@"Enter Description", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 

     switch (tbxLocation.Text) 
     { 
      case "": 
       MessageBox.Show(@"Enter Location", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 
     switch (tbxDiscrepancyDescription.Text) 
     { 
      case "": 
       MessageBox.Show(@"Enter Discrepancy Description", @"Input Needed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       status = true; 
       break; 
     } 
     return status; 
    } 

    public void PrepareEmail(string subject, string message) 
    { 
     if (MessageBox.Show(@"Are you sure you want to save and send Discrepancy Report: " +tbxDRNumber.Text + @"?\n Click YES to save\n Click NO to cancel" , @"Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
     { 

      SendEmail(subject,message); 
     } 
    } 
    public Array AddEmail() 
    { 
     string[] dRemail = { "", "", "" }; 
     if (File.Exists(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml")) 
     { 
      XmlReader emailDocument = new XmlTextReader(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml"); 
      while (emailDocument.Read()) 
      { 
       var type = emailDocument.NodeType; 
       switch (type) 
       { 
        case XmlNodeType.Element: 
         if (emailDocument.Name == "DRCreatedAddEmail") 
         { 
          dRemail[0] = emailDocument.ReadInnerXml(); 
         } 
         if (emailDocument.Name == "DRActionNeededAddEmail") 
         { 
          dRemail[1] = emailDocument.ReadInnerXml(); 
         } 
         if (emailDocument.Name == "DRPendingAddEmail") 
         { 
          dRemail[2] = emailDocument.ReadInnerXml(); 
         } 
         else 
         { 
          MessageBox.Show(
           @"The file: 'DREmailAddresses.xml' was not found at: \\fs01\Applications\EMS-Manager"); 
         } 
         break; 
       } 
      } 
     } 
     return dRemail; 
    } 
+0

不是一個答案,但[文檔的XmlTextReader的說(https://msdn.microsoft.com/en-us/library/1af7xa52(V = vs.110).aspx)say _「從.NET Framework 2.0開始,我們建議您使用XmlReader.Create方法創建XmlReader實例,以利用新功能。」_ – stuartd

+0

查看代碼, t似乎理解開關語句。所有這些都可以通過簡單的陳述來完成。 – LeonG

+0

您對陳述是否正確。修改代碼以使用if語句代替switch語句可能很有用。另外,我對編程和學習仍然很陌生。我自學了一切,我犯了錯誤,這都是學習的一部分。不過,我們下週將在課堂上講述Switch語句。謝謝您的意見。 – TAdams79

回答

1

如果您收到該錯誤信息,那麼你的代碼是越來越近了檢查File.Exists和過去的文檔的閱讀,但只是擊中的其他分支的「if(emailDocument.Name = =「DRPendingAddemail」)「檢查。

所以,你的功能應該是接近這個

public Array AddEmail() 
{ 
    string[] dRemail = { "", "", "" }; 
    if (File.Exists(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml")) 
    { 
     XmlReader emailDocument = new XmlTextReader(@"\\fs01\Applications\EMS-Manager\DREmailAddresses.xml"); 
     while (emailDocument.Read()) 
     { 
      var type = emailDocument.NodeType; 
      switch (type) 
      { 
       case XmlNodeType.Element: 
        if (emailDocument.Name == "DRCreatedAddEmail") 
        { 
         dRemail[0] = emailDocument.ReadInnerXml(); 
        } 
        else if (emailDocument.Name == "DRActionNeededAddEmail") 
        { 
         dRemail[1] = emailDocument.ReadInnerXml(); 
        } 
        else if (emailDocument.Name == "DRPendingAddEmail") 
        { 
         dRemail[2] = emailDocument.ReadInnerXml(); 
        } 
        else 
        { 
         MessageBox.Show("Unknown node type " + emailDocument.Name); 
        } 
        break; 
      } 
     } 
    } 
    else 
    { 
     MessageBox.Show(@"The file: 'DREmailAddresses.xml' was not found at: \\fs01\Applications\EMS-Manager"); 
    } 
    return dRemail; 
} 
+0

謝謝你。我會嘗試你的建議。我甚至沒有意識到它正在檢查所有這些情況。另外,因爲我是新手,所以我如何接受答案? – TAdams79

+0

我已經從switch語句轉換爲if語句,並且得到了相同的結果。 – TAdams79

+0

確保你有我的例子中的其他語句。如果您知道如何在Visual Studio中使用調試器,請在兩個else語句中放置一個斷點並查看執行哪一個。需要注意的一件事是,我添加了一個新的代碼路徑,舊的MessageBox曾用於不同的錯誤消息,因此請確保您看到正確的消息(「找到文件nout」與「未知節點類型」) –