从Web服务检索表到wp7页面

时间:2012-05-06 07:36:04

标签: c# windows-phone-7

有没有办法将表格从Web服务检索到wp7应用程序页面?  我只想比每次获取数据时使用表格更容易使用

1 个答案:

答案 0 :(得分:0)

在MSDN上查看System.Xml命名空间。它包含许多可用的类。您应该使用XmlReader或similair将其加载到数组或泛型集合。希望这会有所帮助,因为有点不清楚。

修改

这是我为将样本数据加载到泛型集合所做的代码:

示例数据:

<?xml version="1.0" encoding="utf-8" ?>
 <studentPunishmentsTables>
  <studentPunishmentsTable>
    <fromSemester/>
    <fromSemesterDesc/>
    <issueDate>01/04/2012</issueDate>
    <note/>
    <penalty>Course Failure</penalty>
    <semester>311</semester>
    <semesterDesc>First Semester 31/32</semesterDesc>
    <toSemester/>
    <toSemesterDesc/>
  </studentPunishmentsTable>
  <studentPunishmentsTable>
    <fromSemester/>
    <fromSemesterDesc/>
    <issueDate>01/04/2012</issueDate>
    <note/>
    <penalty>Semester Failure</penalty>
    <semester>311</semester>
    <semesterDesc>First Semester 31/32</semesterDesc>
    <toSemester/>
    <toSemesterDesc/>
  </studentPunishmentsTable>
</studentPunishmentsTables>

代码:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XMLStudent
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("st.xml");
            List<StudentPunishment> sp = new List<StudentPunishment>();
            foreach (XmlNode nod in doc.SelectNodes(@"studentPunishmentsTables/studentPunishmentsTable"))
            {
                StudentPunishment s = new StudentPunishment();
                s.FromSemester = nod.ChildNodes[0].InnerText;
                s.FromSemesterDesc = nod.ChildNodes[1].InnerText;
                s.IssueDate = nod.ChildNodes[2].InnerText;
                s.Note = nod.ChildNodes[3].InnerText;
                s.Penalty = nod.ChildNodes[4].InnerText;
                s.Semester = nod.ChildNodes[5].InnerText;
                s.SemesterDesc = nod.ChildNodes[6].InnerText;
                s.ToSemester = nod.ChildNodes[7].InnerText;
                s.ToSemesterDesc = nod.ChildNodes[8].InnerText;
                sp.Add(s);
            }
            Console.WriteLine(sp[0].IssueDate);
            Console.Read();
        }
    }
    class StudentPunishment
    {
        public string FromSemester { get; set; }
        public string FromSemesterDesc { get; set; }
        public string IssueDate { get; set; }
        public string Note { get; set; }
        public string Penalty { get; set; }
        public string Semester { get; set; }
        public string SemesterDesc { get; set; }
        public string ToSemester { get; set; }
        public string ToSemesterDesc { get; set; }
    }
}

因此。此代码使用示例数据加载XMLDocument,并将每个studentPunishmentsTable的数据选择为StudentPunishment类的新对象。有保存数据的属性。一切都完成后,学生的惩罚对象被添加到通用集合('List')中,代码试图在集合中显示第一个对象的日期。你可以自己测试,它对我有用。