非调用成员'System.Xml.XmlNode.Attributes'不能像方法一样使用
我是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
可有人请给我一些指点,谢谢提前。
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")
(这似乎在代码中多次)
问题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")
不必要地指定的字符串@
,因为如果他们逃脱与否并不重要。
- 您的foreach循环需要使用与
node
不同的名称,因为它在循环之外使用。 - 您的
if
声明应该具有==
以表明您正在比较两个值而不是将另一个赋值给另一个。 - 无论何时您指的是一组项目,在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函数)。
谢谢大家。在C#上进行了一周的培训,本周我的大脑有点糊涂。我在代码中添加了一些注释,以提醒自己与VB的一些基本差异。非常感谢您的帮助。 – 2012-04-12 23:16:26
你可以向任何答案提出帮助,如果其中一人回答你的问题,你可以将其标记为答案。 – Khan 2012-04-13 15:20:59