Click here to Skip to main content
15,886,100 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi Everyone
I want to convert this code to C#
i test site [^]
but it not work !
VB
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


Could you convert this code to C#?
Thank you.
Posted
Updated 18-May-13 6:58am
v2

You can convert code online

Code Converter
Convert VB.NET to C#
 
Share this answer
 
v2
Comments
Thomas Daniels 18-May-13 13:20pm    
Yes, but then you get an error.
<small> Dim NewRecord As XElement = _
<Question ID=<%= If(ID.Count = 0, 1, CInt(ID.Count) + 1) %> question=<%= question %> answer=<%= answer %>/></small>
is valid VB.NET, but not valid C#. The converter can't convert this statement.
My bad...I just used an online translator without checking it;

I hope this will help you :)


C#
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("")*/
               

}
            }
        }
    }
 
Share this answer
 
v2
Comments
Thomas Daniels 18-May-13 13:20pm    
Reason for my vote of 1: That's not valid C#.
Sina asefi 18-May-13 13:25pm    
First Thank you for your response
but your code is not complete!
charles henington 19-May-13 5:26am    
could you post an example of the xml file your reading?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900