-1

私は 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

誰かが私にいくつかのガイダンスを与えることができますか、事前に感謝します.

4

3 に答える 3

0

1. 「ノード」という名前のローカル変数をこのスコープで宣言することはできません。これは、「ノード」に別の意味を与えるためです。「ノード」は、これらのステートメントの他の何かを示すために「親または現在の」スコープで既に使用されています: (XmlNode ノードノードリスト内)

node変数を2回定義しています

ここ

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")

(これはコードに複数回表示されます)

于 2012-04-12T18:17:42.107 に答える
0

質問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")

@エスケープされているかどうかは問題ではないため、文字列に不必要に を指定しています。

于 2012-04-12T18:18:28.890 に答える
0
  1. foreach ループnodeは、ループの外で使用されるため、別の名前を使用する必要があります。
  2. ステートメントif==、2 つの値を比較していて、一方を他方に代入していないことを示す必要があります。
  3. アイテムの配列を参照するときはいつでも、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;
}

もう 1 つ: このプロジェクトのコンソール アプリケーションを作成しましたが、元のコードは実際には文字列を返す関数でした。Main()メソッドには、VBvoidの a に相当する戻り値の型があります。Subおそらくこれを、文字列を返す C# のメソッド (VB 関数) にする必要があります。

于 2012-04-12T18:25:32.667 に答える