尝试访问for循环内的变量时出错

时间:2015-07-24 08:24:18

标签: c# xml linq

我在下面有一些代码循环遍历XML文档以获取某些信息。我想获取3位信息并将它们放在变量中。由于代码显示我已经在循环之前声明了变量,然后在循环之后我尝试使用变量,它提到它们是未分配的。

我也认为有3个循环可能不是最好的方法,但找不到另一种方法来获取所有三位信息。

继承代码;

private void findDetails(String school)
    {
        XElement root = XElement.Load("proxies.xml");

        IEnumerable<XElement> proxydetails = from el in root.Elements("item") where (string)el.Element("schoolname") == school select el;

        String SchoolAddress1, Port, Enabled = "";

        foreach (XElement el in proxydetails)
        {
            SchoolAddress1 = (string)el.Element("schooladdress");          
        }
        foreach (XElement el in proxydetails)
        {
             Port = (string)el.Element("schoolport");

        }
        foreach (XElement el in proxydetails)
        {
             Enabled = (string)el.Element("schoolenabled");

        }

        MessageBox.Show(SchoolAddress1);
        }

我刚检查了proxydetails中返回的数据,它是以下内容;

<item>
<schoolname>Primary School</schoolname>
<schooladdress>proxy</schooladdress> 
  <schoolport>80</schoolport> 
  <schoolenabled>1</schoolenabled> 
  </item>

以下是现在有效的更新代码;

private void findDetails(String school)
        {
            XElement root = XElement.Load("proxies.xml");

            IEnumerable<XElement> ProxyDetails = from el in root.Elements("item") where (string)el.Element("schoolname") == school select el;

            String schoolAddress1 = "";
            String port = "";
            String enabled = "";

            foreach (XElement el in ProxyDetails)
            {
                schoolAddress1 = (string)el.Element("schooladdress");
                port = (string)el.Element("schoolport");
                enabled = (string)el.Element("schoolenabled");
            }


             MessageBox.Show(schoolAddress1);
            }

3 个答案:

答案 0 :(得分:1)

该行:

String SchoolAddress1, Port, Enabled = "";

相当于:

String SchoolAddress1;
String Port;
String Enabled = "";

如果你想要全部初始化它们,你需要像:

String SchoolAddress1 = "";
String Port = "";
String Enabled = "";

由于编译器无法知道proxydetails是否包含任何内容,因此无法知道是否会在循环中为SchoolAddress1分配值。所以它起到了安全作用,并警告说,在您尝试读取变量时,该变量可能未定义。

答案 1 :(得分:0)

尝试以下代码: -

private void findDetails(string school)
    {
        var root = XElement.Load("proxies.xml");

        IEnumerable<XElement> proxydetails = from el in root.Elements("item") where (string)el.Element("schoolname") == school select el;

        var schoolAddress1 = string.empty;
        var port = string.empty;
        var enabled = string.empty;

        var result = proxydetails.Select(x => new { schoolAddress1 = x.schooladdress, port = x.schoolport, enabled = x.schoolenabled}).ToLost(); 

         MessageBox.Show(result[0].schoolAddress1);
        }

因为在你的代码&#34;&#34;仅被指派给港口; +遵循代码约定以获得更好的编程。

答案 2 :(得分:0)

使用string.Empty值初始化字符串:

String SchoolAddress1, Port, Enabled = "";

应该是

String SchoolAddress1 = string.Empty, Port = string.Empty, Enabled = string.Empty;

此外,将foreach循环合并为一个:

foreach (XElement el in proxydetails)
{
    SchoolAddress1 = (string)el.Element("schooladdress"); 
    Port = (string)el.Element("schoolport");    
    Enabled = (string)el.Element("schoolenabled");
}