使用XML WebApi

时间:2014-02-25 20:25:12

标签: c#

我已经创建了一个Web Api并成功地以json格式获取数据但是当我感到困惑时如何获取xml并用于编程 学生班

namespace StudentInfoClient2
{
    class Student
    {
        public int Id
        {
            get;
            set;
        }
        public string FName
        {
            get;
            set;
        }
        public string LName
        {
            get;
            set;
        }
        public string Gender
        {
            get;
            set;
        }
        public string Course
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public long PhoneNo
        {
            get;
            set;
        }


    }
}

计划类

namespace StudentInfoClient2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The following is the result of Json Data..");
            Method1();
            Console.WriteLine("\n\n\n");
            Console.WriteLine("The following is the result of xml Data...");
            Method2();

        }
        static void Method1()
        {
            using (HttpClient client = new HttpClient())
            {
                List<Student> studlist = new List<Student>();
                client.BaseAddress = new Uri("http://tanmayservice01.sitecloud.cytanium.com/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.GetAsync("api/StudentInfo").Result;
                if (response.IsSuccessStatusCode)
                {
                    studlist = response.Content.ReadAsAsync<List<Student>>().Result;
                    foreach (Student s in studlist)
                    {
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}", s.Id, s.FName, s.LName, s.Email);
                    }
                }
            }
        }
        static void Method2()
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://tanmayservice01.sitecloud.cytanium.com/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

                HttpResponseMessage response = client.GetAsync("api/StudentInfo").Result;
                if (response.IsSuccessStatusCode)
                {
                   // string strxml = response.Content.ReadAsStringAsync().Result;
                    XDocument doc = XDocument.Load(response.Content.ReadAsStreamAsync().Result);
                    List<Student> studlist = new List<Student>();
                    Console.WriteLine(doc.ToString());

               //Linq query iam looking for




                }
            }
        }
    }
}

如何编写Linq查询以获得与json中显示的结果相同的结果?

1 个答案:

答案 0 :(得分:0)

为什么不使用与JSON相同的XML客户端代码?

e.g。

       studlist = response.Content.ReadAsAsync<List<Student>>().Result;
       foreach (Student s in studlist)
       {
           Console.WriteLine("{0}\t{1}\t{2}\t{3}", s.Id, s.FName, s.LName, s.Email);
       }

在这两种情况下,您都有序列化对象,无论序列化是用花括号还是尖括号都没有区别。