本文介绍了将此代码从vb.net转换为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我想将此代码转换为C#

i test []

但它不起作用!

hi Everyone
I want to convert this code to C#
i test site [^]
but it not work !

Private QuestionsAddress As String = Environment.CurrentDirectory & "\Questions.xml"
    Private UnkownAddress As String = Environment.CurrentDirectory & "\Unkown.xml"


    Private Sub AddAQuestion(ByVal question As String, ByVal answer As String)
        Dim document = XDocument.Load(QuestionsAddress)
        'get new id
        Dim ID = From Questions In document.Root.Elements("Question") Select Questions.Attribute("ID") ' document.Descendants.Count
        'make new record
        Dim NewRecord As XElement = _
                <Question ID=<%= If(ID.Count = 0, 1, CInt(ID.Count) + 1) %> question=<%= question %> answer=<%= answer %>/>

        'add record
        document.Root.Add(NewRecord)
        document.Save(QuestionsAddress)
    End Sub

    Private Function FindAnAnswer(ByVal question As String) As String
        Dim document = XDocument.Load(QuestionsAddress)
        Try
            Dim Resault = (From Questions In document.Root.Elements("Question") Where Questions.Attribute("question") = question Select Questions.Attribute("answer")).Single
            Return Resault
        Catch
            'Add Unkonw question
            AddAUnkonwnQuestion(question)

            Return "Motavjeh nashodam ke dar che moredi sohbat mikonid."
        End Try
    End Function

    Private Sub AddAUnkonwnQuestion(ByVal question As String)
        Dim document = XDocument.Load(UnkownAddress)
        'get new id
        Dim ID = From Questions In document.Root.Elements("Question") Select Questions.Attribute("ID") ' document.Descendants.Count
        'make new record
        Dim NewRecord As XElement = _
                <Question ID=<%= If(ID.Count = 0, 1, CInt(ID.Count) + 1) %> question=<%= question %>/>

        'add record
        document.Root.Add(NewRecord)
        document.Save(UnkownAddress)
    End Sub

    Sub Main()

        Dim question As String
        Do
            Console.Write("Your question:")
            question = Console.ReadLine
            If Mid(question, question.Length, 1) = "?" Then
                question = Mid(question, 1, question.Length - 1)
            End If
            If question.Length > 0 Then
                If question.IndexOf("newq:") = 0 Then
                    'Check For Question
                    If question.Split("?").Length > 1 AndAlso (Mid(question, 6).Split("?")(0).Length > 0 And question.Split("?")(1).Length > 0) Then
                        'add new question
                        Try
                            AddAQuestion(Mid(question, 6).Split("?")(0), question.Split("?")(1))
                            Console.WriteLine("question is added")
                            Console.WriteLine("")
                        Catch ex As Exception
                            Console.WriteLine(ex.Message)
                            Console.WriteLine("Example question for add==>newq:is it a question?yes,it's")
                        End Try
                    Else
                        Console.WriteLine("Example question for add==>newq:is it a question?yes,it's")
                    End If
                ElseIf question.IndexOf("unknown:") = 0 Then
                    'View all unknown questions
                    Dim document = XDocument.Load(UnkownAddress)
                    Dim Resault = From Questions In document.Root.Elements("Question") Select Questions.Attribute("question")

                    Dim i As Integer = 1
                    For Each Item In Resault
                        Console.WriteLine("Question " & i & ":" & Item.Value)
                        i += 1
                    Next
                    Console.WriteLine("")
                Else
                    If UCase(question) = UCase("khoroj") Then
                        'exit do
                        Console.WriteLine("Ba tashakor az shoma ke az in chatbot estefade kardid")
                        Console.ReadKey()
                        Exit Do
                    Else
                        'Check question
                        Console.WriteLine("     " & FindAnAnswer(question))
                        Console.WriteLine("")
                    End If

                End If
            End If
        Loop

    End Sub





您能否将此代码转换为C#?

谢谢。



Could you convert this code to C#?
Thank you.

推荐答案


string QuestionsAddress = Directory.GetCurrentDirectory() + "\\Questions.xml";
string UnkownAddress = Directory.GetCurrentDirectory() + "\\Unkown.xml");

private void AddAQuestion(string question, string answer)
{
var document = XDocument.Load(QuestionsAddress);

foreach(var questions in document.GetElementByID("Question"))
{
int ID = questions.GetElementByID("ID");


}

XElement NewRecord = new XElement();
NewRecord =
if(ID.count == 0)
{
ID = 1;

}
for(ID=0;i<=ID.Count;i++)
{
question[i]+=question;
answer[i]+=answer;
}
AddAUnkonwnQuestion(question);
}

 private void AddAUnkonwnQuestion(string question)
    {
        var document = XDocument.Load(UnkownAddress);
        document.Root.Add(NewRecord)
        document.Save(UnkownAddress)
    }

void Main()
{
        string question;
        for (
        ; // TODO: Warning!!!! NULL EXPRESSION DETECTED...
        ;
        ) {
            Console.Write("Your question:");
            question = Console.ReadLine();
            if ((question.Substring((question.Length - 1), 1) == "?"))
            {
                question = question.Substring(0, (question.Length - 1));
            }
            if ((question.Length > 0)) {
                if ((question.IndexOf("newq:") == 0))
               {
                    /*Check For Question
                    If question.Split("?").Length > 1 AndAlso (Mid(question, 6).Split("?")(0).Length > 0 And question.Split("?")(1).Length > 0) Then
                        'add new question*/
                }
 		try {
            AddAQuestion(question.Substring(5).Split("?")[0], question.Split("?")[1]);
            Console.WriteLine("question is added");
            Console.WriteLine("");
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
            Console.WriteLine("Example question for add==>newq:is it a question?yes,it\'s");
        }
        Console.WriteLine("Example question for add==>newq:is it a question?yes,it\'s");
    }
else
{
question.IndexOf("unknown:") = 0
/*
 View all unknown questions
                    var document = XDocument.Load(UnkownAddress)
                   foreach(Questions In document.GetElementByName("Question"))
{
var result = Questions.GetElementByName("question");
}

                   int i = 1
                    foreach(var item in result)
{
                        Console.WriteLine("Question " & i & ":" & Item.Value)
                        i += 1
                    Console.WriteLine("");
}

         if(question.ToUpper()) = "khoroj".ToUpper() */
	{
	Console.WriteLine("Ba tashakor az shoma ke az in chatbot estefade kardid");
        Console.ReadKey();
	}

 /*Check question
                        Console.WriteLine("     " & FindAnAnswer(question))
                        Console.WriteLine("")*/


}
            }
        }
    }


这篇关于将此代码从vb.net转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 14:33