如何使从XML中提取的信息在当前上下文之外可用?

时间:2015-02-26 04:22:34

标签: c# xml

我有这个方法读取特定的.xml文件并将一些信息存储在两个字符串中。

但是我无法从该方法外部访问这些字符串。

在button1_Click事件中,两个字符串都无法访问(当前上下文错误中不存在)

如何解决这个问题?这是代码:

private void checkXML(string _title)
    {
        using (XmlReader reader = XmlReader.Create(@"C:\Directory\test.xml"))
        {
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                        case "myElement":
                            string title = reader["title"];
                            if (title == _title)
                            {
                                string string1 = (reader["publisher"]);
                                string string2 = (reader["author"]);
                            }
                            break;
                    }
                }
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        checkXML("The Cookbook");
        MessageBox.Show(string1); //error: "The name 'string1' does not exist in the current context"
        MessageBox.Show(string2); //error: "The name 'string2' does not exist in the current context"
    }

提前致谢!

2 个答案:

答案 0 :(得分:1)

将两个变量添加为类字段:

public class SomeClass : SomeBaseClass
{

 string string1="";
 string string2 ="";  // defined on class level

private void checkXML(string _title)
    {
        using (XmlReader reader = XmlReader.Create(@"C:\Directory\test.xml"))
        {
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                        case "myElement":
                            string title = reader["title"];
                            if (title == _title)
                            {
                                string1 = (reader["publisher"]);
                                string2 = (reader["author"]); // set values here
                            }
                            break;
                    }
                }
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        checkXML("The Cookbook");
        MessageBox.Show(string1); //Now aviable here"
        MessageBox.Show(string2); //Available here"
    }
}

答案 1 :(得分:0)

试试这样。为结果创建新类型,并从填充值的方法返回该类型。在调用方法中使用该值。

class XmlResult
{
    public string string1 {get; set;}
    public string string2 {get; set;}
}

private XmlResult checkXML(string _title)
{
    var result = new XmlResult();
    using (XmlReader reader = XmlReader.Create(@"C:\Directory\test.xml"))
    {
        while (reader.Read())
        {
            if (reader.IsStartElement())
            {
                switch (reader.Name)
                {
                    case "myElement":
                        string title = reader["title"];
                        if (title == _title)
                        {
                            result.string1 = (reader["publisher"]);
                            result.string2 = (reader["author"]);
                        }
                        break;
                }
            }
        }
    }
    return result;
}

private void button1_Click(object sender, EventArgs e)
{
    var result = checkXML("The Cookbook");
    MessageBox.Show(result.string1); //error: "The name 'string1' does not exist in the current context"
    MessageBox.Show(result.string2); //error: "The name 'string2' does not exist in the current context"
}