我是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