我是 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)
{
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' 不能像 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
有人可以给我一些指导,提前谢谢。