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

本文关键字:方法 一样 不能 成员 调用 System Attributes XmlNode Xml | 更新日期: 2023-09-27 18:31:07

我是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"已经在"父或当前"作用域中用于表示这些语句的其他内容:(nodeList 中的 XmlNode 节点)2. 不可调用的成员"System.Xml.XmlNode.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

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

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

  1. foreach 循环需要使用与 node 不同的名称,因为它是在循环外部使用的。
  2. 您的if语句应该具有==,以表明您正在比较两个值,而不是将一个值分配给另一个值。
  3. 每当引用项数组时,在 C# 中都使用 [] 而不是 () 。 这将解决您不能像方法一样使用的问题。

尝试这样的事情:

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 函数)。

1. 不能在此作用域中声明名为"node"的局部变量,因为它会给"node"赋予不同的含义,该"node"已经在"父或当前"作用域中用于表示这些语句的其他内容: (节点列表中的 XmlNode 节点)

您将变量定义两次node

这里

XmlNode node = null;

和这里:

foreach (XmlNode node in nodeList)

在您的foreach中命名node其他内容.


2. 不可调用的成员"System.Xml.XmlNode.Attributes"不能像节点的方法一样使用。属性线。

您在应该使用方括号的地方使用了括号。
改变 if (node.Attributes(@"Name").InnerText = @"Something")

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

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

问题 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")

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