-1

我是 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. 无法在此范围内声明名为“节点”的局部变量,因为它会给“节点”赋予不同的含义,“节点”已在“父或当前”范围中用于表示其他内容对于这些语句:(nodeList 中的 XmlNode 节点) 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

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

4

3 回答 3

0

1. 不能在此范围内声明名为“节点”的局部变量,因为它会给“节点”赋予不同的含义,“节点”已在“父或当前”范围中用于为这些语句表示其他内容:(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")

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

于 2012-04-12T18:17:42.107 回答
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")

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

于 2012-04-12T18:18:28.890 回答
0
  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返回类型,它相当于SubVB 中的 a。您可能应该将其变成 C# 中返回字符串的方法(VB 函数)。

于 2012-04-12T18:25:32.667 回答