2012-04-12 98 views
-1

我是C#的新手,並試圖轉換VB.NET應用程序。使用此代碼:非調用成員'System.Xml.XmlNode.Attributes'不能像方法一樣使用

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'。屬性行。

的一部開拓創新的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 

可有人請給我一些指點,謝謝提前。

回答

0

1.一個名爲'node'的局部變量不能在這個範圍內聲明,因爲它會給'node'賦予不同的含義,'node'已經在'parent或current'範圍內用來表示其他的東西聲明:(在節點列表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")

(這似乎在代碼中多次)

+0

謝謝大家。在C#上進行了一週的培訓,本週我的大腦有點糊塗。我在代碼中添加了一些註釋,以提醒自己與VB的一些基本差異。非常感謝您的幫助。 – 2012-04-12 23:16:26

+0

你可以向任何答案提出幫助,如果其中一人回答你的問題,你可以將其標記爲答案。 – Khan 2012-04-13 15:20:59

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

不必要地指定的字符串@,因爲如果他們逃脫與否並不重要。

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,與VB中的Sub等效。你應該把它變成一個在C#中返回一個字符串的方法(VB函數)。

相關問題