不可调用的成员'System.Xml.XmlNode.Attributes'不能像方法一样使用

时间:2012-04-12 18:14:19

标签: c# vb.net

我是C#的新手并尝试转换VB.NET应用。使用此代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace TestXML
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDataDocument Doc = new XmlDataDocument();
            XmlNodeList nodeList;
            XmlElement Element;
            XmlNode node = null;
            Doc.Load(@"UNC path of a doc.xml file");
            Element = Doc.DocumentElement;
            nodeList = Element.SelectNodes("Application");
            foreach (XmlNode node in nodeList)
            {
                if (node.Attributes(@"Name").InnerText = @"Something")
                    break;
            }
            //gsCurrentMode is one of "Production","Test","Develope"
            nodeList = node.SelectNodes("Instance");
            foreach (XmlNode n in nodeList)
            {
                if (node.Attributes("Mode").Value = @"Production")
                    //if either of these two fails, Something shuts down
                    return node.Attributes("Server").InnerText;
                else
                {
                    return;
                }
            }       
        }
    }
}

我收到以下错误: 1.名为'node'的局部变量不能在此范围内声明,因为它会给'node'赋予不同的含义,'node'已经在'parent或current'范围内用于表示这些语句的其他内容:(XmlNode node在nodeList中) 2.不可调用的成员'System.Xml.XmlNode.Attributes'不能像node.Attributes行的方法一样使用。

原始VB.NET代码如下:

Public Function GetProductionServer() As String
        Dim Doc As New XmlDocument
        Dim nodeList As XmlNodeList
        Dim Element As XmlElement
        Dim node As XmlNode = Nothing
        Doc.Load("UNC Path to an Doc.xml")
        Element = Doc.DocumentElement
        nodeList = Element.SelectNodes("Application")
        For Each node In nodeList
            If node.Attributes("Name").InnerText = "Something" Then
                Exit For
            End If
        Next
        '--- gsCurrentMode is one of "Production","Test","Develope"
        nodeList = node.SelectNodes("Instance")
        For Each node In nodeList
            If node.Attributes("Mode").Value = "Production" Then
                '-- if either of these two fails, Something shuts down
                Return node.Item("Server").InnerText
            End If
        Next
        Return ""
    End Function

有人可以给我一些指导,谢谢你。

3 个答案:

答案 0 :(得分:0)

1。名为'node'的局部变量不能在此范围内声明,因为它会给'node'赋予不同的含义,'node'已经在'parent或current'范围内用于表示这些语句的其他内容:(nodeList中的XmlNode节点) )

您要定义变量node两次

下面

XmlNode node = null;

和这里:

foreach (XmlNode node in nodeList)

node中的其他名称foreach


2。不可调用的成员'System.Xml.XmlNode.Attributes'不能像node.Attributes行的方法一样使用。

您正在使用括号,您应该使用方括号 更改 if (node.Attributes(@"Name").InnerText = @"Something")

if (node.Attributes[@"Name"].InnerText = @"Something")

(这在您的代码中多次出现)

答案 1 :(得分:0)

问题1

您无法在函数中重用变量名称,您需要将其定义为其他名称,所以:

foreach (XmlNode node in nodeList)
{
    if (node.Attributes(@"Name").InnerText = @"Something")
        break;
}

应该是:

foreach (XmlNode nn in nodeList)
{
    if (n.Attributes(@"Name").InnerText = @"Something")
        break;
}

问题2

此:

return node.Attributes("Server").InnerText;

应该是:

return node.Attributes["Server"].InnerText;

作为一个例子。

您使用node.Attributes(*)的任何地方都应该是node.Attributes[*]。在VB中,使用与方法调用相同的语法调用索引器。在C#中,我们在索引器中使用括号('[',']')。


调整后的代码:

static void Main(string[] args)
{
    XmlDataDocument Doc = new XmlDataDocument();
    XmlNodeList nodeList;
    XmlElement Element;
    XmlNode node = null;
    Doc.Load(@"UNC path of a doc.xml file");
    Element = Doc.DocumentElement;
    nodeList = Element.SelectNodes("Application");
    foreach (XmlNode n in nodeList)
    {
        if (n.Attributes[@"Name"].InnerText = @"Something")
            break;
    }
    //gsCurrentMode is one of "Production","Test","Develope"
    nodeList = node.SelectNodes("Instance");
    foreach (XmlNode n in nodeList)
    {
        if (node.Attributes["Mode"].Value = @"Production")
            //if either of these two fails, Something shuts down
            return node.Attributes["Server"].InnerText;
        else
        {
            return;
        }
    }       
}

另请注意:

if (node.Attributes[@"Name"].InnerText = @"Something")

不必要地指定字符串上的@,因为它们是否被转义并不重要。

答案 2 :(得分:0)

  1. 您的foreach循环需要使用与node不同的名称,因为它在循环之外使用。
  2. 您的if语句应==,以表明您正在比较两个值,而不是将两个值分配给另一个。
  3. 每当您引用一系列项目时,在C#中使用[]代替()。这将解决您的无法像方法一样使用的问题。
  4. 尝试这样的事情:

    foreach (XmlNode n in nodeList)
    {
        if (n.Attributes["Name"].InnerText == "Aurora NET")
        //NOTE: You've found the node, but you aren't actually doing anything here.
        break;
    }
    

    另一件事:您已为此项目创建了一个控制台应用程序,但您的原始代码实际上是一个返回字符串的函数。 Main()方法的返回类型为void,相当于VB中的Sub。你应该把它变成C#中一个返回字符串的方法(VB函数)。

相关问题