Windows Phone 7中的XML到本地数据库

时间:2012-12-19 12:03:43

标签: c# xml windows-phone-7 windows-phone-7.1 nsxmlparser

从我的服务器下载xml文件时会出现此函数(外观代码)。现在我想在本地数据库中加载它...

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();
            }
        }    
    }

我有两个问题: 1)如何阅读xml内容并将其写入:

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

2)for (int i = 0; i < quest.Length; i++)这里quest.Length给出错误(我知道为什么)...如何使自定义函数获得长度?我需要它来计算<Questions>标签

1 个答案:

答案 0 :(得分:2)

您的xaml格式不正确,因为它有多个根元素(问题)。

假设它的格式是否正确(我有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>";

您可以拥有上述任意数量的标签

现在,您可以将此数据转换为类,如下所示

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();

这里我的问题类是这样的

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

现在以适用的方式将上述过程应用于您的数据