C#访问在先前方法中创建的方法中的数组

时间:2018-05-23 10:26:33

标签: c# arrays

声明一个公共静态数组,并在单击按钮内创建此数组变量的实例。我有一个for循环,它遍历一个XMLNodeList变量,并为数组中的每个申请人标签存储XML。第一次运行时,它从XMLNodeList中选取某些字段并在前端显示OK - 当我第二次调试它运行方法时,它尝试访问数组字符串,当我调试数组中的所有值时是空的。有一些我使用stackoverflow遵循的例子,但没有找到帮助我解决上述问题的有用的东西。

XML示例:

<applicant app-type=\"applicant\" designation=\"all\" sequence=\"1\" >
        <name>Test</name>
        <address>
            <address-1>123</address-1>
            <address-2>Quality St</address-2>
            <country>JP</country>
        </address>
    </addressbook>
    <nationality>
        <country />
    </nationality>
    <residence>
        <country />
    </residence>
</applicant>
<applicant app-type=\"applicant\" designation=\"all\" sequence=\"2\" >
    <name>Test</name>
    <address>
        <address-1>456</address-1>
        <address-2>Butter Road</address-2>
        <country>AM</country>
    </address>
    </addressbook>
    <nationality>
        <country />
    </nationality>
    <residence>
        <country />
    </residence>
</applicant>


public static string[] eachXML;
private static int sequenceCounter;
public static int totalNumberSequences;

  protected void QueryEPO_Click(object sender, EventArgs e)
    {

XMLDocument xmlDoc2 = new XmlDocument();
xmlDoc2.LoadXml(applicantXML);
XmlNodeList elemList2 = xmlDoc2.GetElementsByTagName("applicant");
for (int i = sequenceCounter - 1; i < totalNumberSequences; i++)
{
eachXML[i] = elemList2.Item(i).OuterXml;

}

populateFields(sequenceCounter - 1);

}

 private void populateFields(int sequenceCounter)
{

    if (sequenceCounter < totalNumberSequences)
    {
        XmlDocument xmlDoc3 = new XmlDocument();
        xmlDoc3.LoadXml(eachXML[sequenceCounter]);

        if (xmlDoc3.GetElementsByTagName("name")[0] == null)
        {
            string name = "";
            nametextbox.Value = name;
        }
        else
        {
            string name = xmlDoc3.GetElementsByTagName("name")[0].InnerText;
            nametextbox.Value = name;
        }
}

}

第一个方法运行图像: enter image description here

第二种方法运行图像: enter image description here

1 个答案:

答案 0 :(得分:0)

public class Xmlitem
{
    public static string[] eachXML { get; set; }
}

我添加了这个类并将我的实例更改为:

Xmlitem.eachXML = new string[totalNumberSequences]