【发布时间】:2012-10-31 10:42:02
【问题描述】:
我正在调用一个 web 服务,它返回带有学生的 XML。我需要将所有学生存储在我的数据库中(现有的学生表)。
现在我有一个获取 XML 的 XMLStudentParser 类,但是我不知道如何继续将每个学生记录存储到数据库中。我是否使用 XMLReader 循环遍历学生并将每个学生添加到 List<Student>,然后将该列表保存到数据库中?
通过网络服务远程 XML。
<Response>
<Result>True</Result>
<Table>
<Students>
<Student>
<StudentID>14165</StudentID>
<StudentName>Jeff Smith</StudentName>
<GroupId>9109</GroupId>
</Student>
<Student>
<StudentID>14168</StudentID>
<StudentName>Mary Jones</StudentName>
<GroupId>9109</GroupId>
</Student>
</Students>
</Table>
</Response>
我的学生模型
public class Student
{
public int StudentId { get; set; }
public string FullName { get; set; }
public int GrpId { get; set; }
}
最佳实践代码看起来如何:
- 从网络服务获取 XML
- 解析每个学生
- 将每个学生存储在数据库中
【问题讨论】:
-
您遗漏了有关 WebService 和数据库 API 的详细信息。这使得详细的答案变得困难。
-
学生是否已经存在于数据库中?
-
我基本上可以访问远程 XML 文件; 1个给家长,1个给学生,1个给他们在学校上的课。我需要定期在数据库中导入信息。 XML 可通过 http 调用访问。学生不会被手动添加,只是通过导入,所以我可以在必要时调整表格以适应导入——我只是不知道处理这个问题的正确方法(来自 RoR 背景)
-
顺便说一句,我首先要导入父母,然后导入学生并通过 parentID 将他们链接到现有父母.. brr.
标签: c# sql xml asp.net-mvc-4