C# 非发票会员';System.Xml.XmlNode.Attributes';不能像方法一样使用
我是C#新手,正在尝试转换VB.NET应用程序。使用此代码:C# 非发票会员';System.Xml.XmlNode.Attributes';不能像方法一样使用,c#,vb.net,C#,Vb.net,我是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)
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 node)
2.非invocable成员“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
有人能给我一些指导吗?先谢谢你。1。无法在此作用域中声明名为“node”的局部变量,因为它将赋予“node”不同的含义,而“node”已在“父或当前”作用域中用于表示这些语句的其他内容:(nodeList中的XmlNode node)
您将定义变量节点
两次
这里
XmlNode=null代码>
在这里:
foreach (XmlNode node in nodeList)
命名节点
您的foreach中的其他内容
二,。非invocable成员“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;
举个例子
几乎可以在任何使用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循环需要使用与节点不同的名称,因为它是在循环外部使用的李>
您的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的一些基本区别。非常感谢您的帮助。您可以对任何帮助您的答案进行投票,如果其中一个答案回答了您的问题,您可以将其标记为答案。
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;
}