0

そのため、ユーザーにファイルの場所を入力するように求めるプロンプトが表示されます。ファイルがそこにない場合、プログラムがユーザーにファイルの場所を再確認するようにするにはどうすればよいですか? これが私の現在のコードです。いじっていたので、元の形で持っています。私はちょっと近づいていましたが、葉巻はありませんでした。前もって感謝します!

編集: したがって、ユーザーが c:/file.xml と入力し、それが c:/file32.xml である必要がある場合、クラッシュします。ユーザーに再試行を求めるにはどうすればよいですか?

static void Main(string[] args)
    {
        //asks user for location of the XML file
        Console.WriteLine("Enter location of XML file and XML file name please. Example - c:/xmlfile.xml");
        //XML files becomes "xmlfile"
        string xmlfile = Console.ReadLine();


        //takes xmlfile and runs it through XPATH
        string fileName = xmlfile;
        XPathDocument doc = new XPathDocument(fileName);
        XPathNavigator nav = doc.CreateNavigator();

        //Compile a standard XPath expression
        //Selects all of the names
        XPathExpression expr;
        expr = nav.Compile("/zabbix_export/templates/template/items/item/name | /zabbix_export/templates/template/items/item/description");
        XPathNodeIterator iterator = nav.Select(expr);
4

3 に答える 3

2
while (!File.Exists(xmlFile))
{
       Console.WriteLine("Please reenter a valid file name:");
        xmlfile = Console.ReadLine();
}
于 2012-06-22T20:33:31.887 に答える
1

ユーザーが最終的に正しいパスを入力するまで、ファイルが存在するかどうかを確認し続けてください:)

string xmlFile;
do
{
    Console.WriteLine("Enter location of XML file and XML file name please. Example - c:/xmlfile.xml");
    xmlFile = Console.ReadLine();
} while (!File.Exists(xmlFile));
于 2012-06-22T20:31:57.950 に答える
1
XPathDocument doc = new XPathDocument(fileName);

while (doc == null)
{
    // display prompt
    // try to retrieve new value for doc
}

    XPathNavigator nav = doc.CreateNavigator();
    //Compile a standard XPath expression
    //Selects all of the names
    XPathExpression expr;
    expr = nav.Compile("/zabbix_export/templates/template/items/item/name | /zabbix_export/templates/template/items/item/description");
    XPathNodeIterator iterator = nav.Select(expr);
于 2012-06-22T20:32:18.027 に答える