a1204773
a1204773

Reputation: 7043

XML to local database in windows phone 7

This function (look CODE) occur when xml file is downloaded from my server.. Now I want to load it in my local database...

XML

<?xml version="1.0" encoding="utf-8" ?>
<Questions>
  <idQuestion>1</idQuestion>
  <Question>Question 1</Question>
  <CorrectAns>Ans1</CorrectAns>
  <WrongAns1>Ans2</WrongAns1>
  <WrongAns2>Ans3</WrongAns2>
  <WrongAns3>Ans4</WrongAns3>
  <Category>CategoryName</Category>
</Questions>
<Questions>
  <idQuestion>2</idQuestion>
  <Question>Question 2</Question>
  <CorrectAns>Ans1</CorrectAns>
  <WrongAns1>Ans2</WrongAns1>
  <WrongAns2>Ans3</WrongAns2>
  <WrongAns3>Ans4</WrongAns3>
  <Category>CategoryName</Category>
</Questions>
etc...

CODE:

    void downloader_Completed(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XElement quest = XElement.Parse(e.Result);

        using (QuestionContext context = new QuestionContext(ConnectionString))
        {
            for (int i = 0; i < quest.Length; i++)
            {
                Question q = new Question();
                q.idQuestion = ; 
                q.Question = ;
                q.CorrectAns = ;
                q.WrongAns1 = ;
                q.WrongAns2 = ;
                q.WrongAns3 = ;
                context.QuestionsDB.InsertOnSubmit(q);
                context.SubmitChanges();
            }
        }    
    }

I have 2 questions: 1)How to read the xml content and write them in:

 q.idQuestion = ; 
 q.Question = ;
 q.CorrectAns = ;
 q.WrongAns1 = ;
 q.WrongAns2 = ;
 q.WrongAns3 = ;

2)for (int i = 0; i < quest.Length; i++) Herequest.Length give error (I know why)... How to make custom function to get the length? I need it to count <Questions> tags

Upvotes: 1

Views: 298

Answers (1)

nkchandra
nkchandra

Reputation: 5557

Your xaml is not in valid format, because it has multiple root elements (Questions).

Assuming if it is in the correct format like this (I have modifiec)

string xmlData = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Questions>
  <Question>
    <idQuestion>1</idQuestion>
    <QuestionName>Question 1</QuestionName>
    <CorrectAns>Ans1</CorrectAns>
    <WrongAns1>Ans2</WrongAns1>
    <WrongAns2>Ans3</WrongAns2>
    <WrongAns3>Ans4</WrongAns3>
    <Category>CategoryName</Category>
  </Question>
  <Question>
    <idQuestion>2</idQuestion>
    <QuestionName>Question 2</QuestionName>
    <CorrectAns>Ans1</CorrectAns>
    <WrongAns1>Ans2</WrongAns1>
    <WrongAns2>Ans3</WrongAns2>
    <WrongAns3>Ans4</WrongAns3>
    <Category>CategoryName</Category>
  </Question>
</Questions>";

You can have any number of tags as above

Now, you can convert this data into classes as shown below

XElement quest = XElement.Parse(xmlData);

        var questionsData = from qn in quest.Descendants("Question")
                           select new Question
                           {
                               idQuestion = int.Parse(qn.Element("idQuestion").Value),
                               QuestionName = qn.Element("QuestionName").Value,
                               CorrectAns = qn.Element("CorrectAns").Value,
                               WrongAns1 = qn.Element("WrongAns1").Value,
                               WrongAns2 = qn.Element("WrongAns2").Value,
                               WrongAns3 = qn.Element("WrongAns3").Value
                           };

        var Questions = questionsData.ToList();
        var noofquestions = Questions.Count;
        //If you want any question with specific id, say '2'
        int idQuestion = 2;
        var question = Questions.Where(item => item.idQuestion == idQuestion).First();

Here my Question class is like this

class  Question
    {
        public int idQuestion ; 
        public string QuestionName ;
        public string CorrectAns ;
        public string WrongAns1 ;
        public string WrongAns2 ;
        public string WrongAns3 ;
    }

Now apply the above process to your data, in whatever way applies

Upvotes: 2

Related Questions