我是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
可有人請給我一些指點,謝謝提前。
謝謝大家。在C#上進行了一週的培訓,本週我的大腦有點糊塗。我在代碼中添加了一些註釋,以提醒自己與VB的一些基本差異。非常感謝您的幫助。 – 2012-04-12 23:16:26
你可以向任何答案提出幫助,如果其中一人回答你的問題,你可以將其標記爲答案。 – Khan 2012-04-13 15:20:59