为什么string []被识别为字符串

时间:2014-11-24 15:05:57

标签: c# xml winforms class-structure

我试图将我的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];
    }

我想将数组带入我的Math类并将时间转换为十进制值进行计算。我的Math类似乎将其识别为字符串而不是数组。

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

        string startTime = "";

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

我希望Math.cs中的weekStart抛出错误。为什么这不会引起错误?

我在UI对话框DetailerReport上调用startHour()

    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>
  .....

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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变量。

此外,我将for循环中的转义更改为i < nodeStart.Count