2014-11-24 132 views
-4

我試圖讓我的XML類與我的Math類分開。我在讀一個xml文件尋找什麼時候開始的人,並把在一個字符串[](我想?)爲什麼字符串[]被識別爲只是一個字符串

public static string elementStartWeek() 
    { 
     XmlDocument xmldoc = new XmlDocument(); 

     fileExistsWeek(xmldoc); 

     XmlNodeList nodeStart = xmldoc.GetElementsByTagName("Start"); 

     int count2 = 0; 
     string[] starttxtWeek = new string[count2]; 

      for (int i = 0; i <= nodeStart.Count; i++) 
      { 
       starttxtWeek[count2] = nodeStart[i].InnerText; 
       count2++; 
      } 

     return starttxtWeek[count2]; 
    } 

我希望把數組轉換成我的數學課和時間轉換用於計算的十進制值。我的數學課似乎認識到這是一個字符串,而不是一個數組。

public static void startHour() 
    { 
     string weekStart = WidgetLogic.elementStartWeek(); 

     string startTime = ""; 

     if (1 == 1) 
     { 
      startTime = weekStart; 
      MessageBox.Show(weekStart); 
     } 
    } 

我本來期望Math.cs中的weekStart發生錯誤。爲什麼這不會引發錯誤?

我正在調用startHour()在UI對話框DetailerReport

public DetailerReports() 
    { 
     InitializeComponent(); 

     Math.startHour(); 
    } 

EDIT1這是XML結構

<?xml version="1.0" encoding="utf-8"?> 
<Form1> 
    <Name Key="11/19/2014 11:26:13 AM"> 
    <Date>11/19/2014</Date> 
    <JobNum></JobNum> 
    <RevNum></RevNum> 
    <Task></Task> 
    <Start>11:26 AM</Start> 
    <End>11:26 AM</End> 
    <TotalTime>55870781</TotalTime> 
    </Name> 
    ..... 
+4

爲什麼會期待一個錯誤?您從方法返回一個字符串並將其分配給字符串。 – 2014-11-24 15:08:17

+0

我認爲也許'弗蘭克Pytel'你應該讀一下'string && string []'[string](http://msdn.microsoft.com/en-us/library/362314fe.aspx)&& [字符串[]'](http://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx) – MethodMan 2014-11-24 15:13:25

回答

6

你的方法返回只是一個string不是一個數組。這是第一個問題,第二個問題是你用0初始化數組。

public static string[] elementStartWeek() 
    { 
     XmlDocument xmldoc = new XmlDocument(); 

     fileExistsWeek(xmldoc); 

     XmlNodeList nodeStart = xmldoc.GetElementsByTagName("Start"); 

     int count2 = 0; 
     string[] starttxtWeek = new string[nodeStart.Count]; 

      for (int i = 0; i < nodeStart.Count; i++) 
      { 
       starttxtWeek[i] = nodeStart[i].InnerText; 
       count2++; 
      } 

     return starttxtWeek; 
    } 
+2

也爲任何其他人來改變'for( )'循環讀取'(int i = 0; i 2014-11-24 16:03:05

2

你只返回一個字符串,而不是它的一個數組。改變你的功能是這樣的:

public static string[] elementStartWeek() 
{ 
    XmlDocument xmldoc = new XmlDocument(); 

    fileExistsWeek(xmldoc); 

    XmlNodeList nodeStart = xmldoc.GetElementsByTagName("Start"); 

    string[] starttxtWeek = new string[nodeStart.Count]; 

     for (int i = 0; i < nodeStart.Count; i++) 
     { 
      starttxtWeek[i] = nodeStart[i].InnerText; 
     } 

    return starttxtWeek; 
} 

此外,您必須將數組容量設置爲nodeStart.Count。你將不需要count2變量。

此外,我改變了contition在for循環i < nodeStart.Count

+0

帕維爾,你仍然錯過了'新字符串[count2];'邏輯...'他的代碼是錯誤的... – 2014-11-24 15:10:35

+1

@JohnBus​​tos但是這是讓它拋出錯誤的那個。我也在上週與這場戰鬥。我必須將'elementStartWeek()'定義爲'string []'來獲得智能感知。至少現在我可以繼續調試。 :-D – 2014-11-24 15:12:30

+1

@FrankPytel修復starttxtWeek的維度,string [] starttxtWeek = new string [nodeStart.Count]; – Monah 2014-11-24 15:13:55

相關問題