从我的服务器下载 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...
代码:
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();
}
}
}
我有 2 个问题: 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>
标签